有表A:
AID, Title
1, test
2, test
3, test表B:
BID, AID, DESC
1, 2, test cc
2, 2, test cc
3, 2, test cc
4, 1, test cc我现在想用一条查询语句得到表A的数据,其中包含表B的数量
如:
AID, Title, Count
1, test, 1
2, test, 3
3, test, 0该如何写查询呢?
AID, Title
1, test
2, test
3, test表B:
BID, AID, DESC
1, 2, test cc
2, 2, test cc
3, 2, test cc
4, 1, test cc我现在想用一条查询语句得到表A的数据,其中包含表B的数量
如:
AID, Title, Count
1, test, 1
2, test, 3
3, test, 0该如何写查询呢?
SELECT a.aid,a.title,COUNT(*) AS num FROM a JOIN b ON a.aid=b.aid
GROUP BY a.`id`ORSELECT * FROM a JOIN b ON a.aid=b.aid
from a left join b on a.AID=B.AID
group by a.AID, a.Title