我正在阅读这篇文章,我正在尝试理解这个SQL语句,但我仍然对SQL有些新意.
我不确定评论和c是什么意思.
我认为其中一个是表名,但我不确定另一个.此外,显然其中有一个子查询,我没有任何经验:
SELECT c.id, c.user_id, c.body, c.deep, c.lineage, c.parent_id,
(SELECT COUNT(*)
FROM comment
WHERE comment.lineage LIKE (CONCAT(c.lineage,'%'))
AND comment.lineage != c.lineage) AS replies
FROM comment as c
ORDER BY c.lineage
Run Code Online (Sandbox Code Playgroud)
SELECT c.id,
c.user_id,
c.body,
c.deep,
c.lineage,
c.parent_id, (
SELECT COUNT(*)
FROM comment
where comment.lineage LIKE (CONCAT(c.lineage,'%'))
AND comment.lineage!=c.lineage)
as replies
FROM comment as c
order by c.linea
Run Code Online (Sandbox Code Playgroud)
第一个列表是所有需要选择的字段,其前缀c是后面表格的别名comment。
查询中的查询是子查询,它运行执行类似操作并.clineage与%(通配符)连接的查询。该子查询结果保存在replies.
结果按 排序linea。