数据库语句问题
发布网友
发布时间:2022-05-18 07:29
我来回答
共1个回答
热心网友
时间:2023-10-11 08:07
假设居民表J,房子表F,车子表C
设计思路如下2种:
1、SELECT J.*,
(SELECT COUNT(*) FROM F WHERE F.RESIDENTID=J.ID) AS 房子数,
(SELECT COUNT(*) FROM C WHERE C.RESIDENTID=J.ID)车子数
FROM J;
2、SELECT J.(你需要的列名),
COUNT(f.RESIDENTID) AS 房子数,
COUNT(C.RESIDENTID) AS 车子数
FROM J LEFT JOIN F ON F.RESIDENTID=J.ID
LEFT JOIN C ON C.RESIDENTID=J.ID
GROUP BY J.(你需要的列名);