Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有表“树”。
我有查询:SELECT * FROMtree WHEREpid=10 此查询返回 10 个项目。
SELECT * FROM
WHERE
=10
我想得到类似的结果:
id | pid | title | subElements 11 | 10 | t 1 | 12 12 | 10 | t 2 | 16 13 | 10 | t 3 | 0 ...
如何构建连接查询来计算这 10 个项目的子项目?
试试这个:
SELECT t1.id, t1.pid, t1.title , count(t2) as subElements FROM tree as t1 LEFT JOIN tree as t2 ON t2.pid = t1.id WHERE t1.pid=10 GROUP BY t1.id, t1.pid, t1.title