I'm reading this article and I'm trying to understand this SQL statement but I am still somewhat new to SQL.
I'm not sure what comment and c refer to.
I think one of them is the table name but I am not sure of the other. Also, apparently there is a subquery within it which I have not had any experience with:
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
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
The first list are all the fields to be selected, with the prefix of c
which is the alias later to the comment
table.
The query in a query is a subquery, which runs that query which does a like and concatenates .clineage
with %
(which is the wildcard). This subquery result is saved in replies
.
The results are ordered by linea
.