0

我有表“树”。

我有查询:SELECT * FROMtree WHEREpid=10 此查询返回 10 个项目。

我想得到类似的结果:

id | pid | title | subElements
11 | 10  | t 1   | 12
12 | 10  | t 2   | 16
13 | 10  | t 3   | 0
...

如何构建连接查询来计算这 10 个项目的子项目?

4

1 回答 1

1

试试这个:

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
于 2012-05-10T11:09:32.940 回答