考虑下面给出的保险数据库:
person (driver-id, name, address)<br>
car (license, model, year)<br>
accident (report-number, date, location)<br>
owns (driver-id, license) <br>
participated (driver-id, car, report-number, damage-amount)<br>
1)查找2004年拥有涉及事故的汽车的总人数。
这是我想出的:
SELECT COUNT(driver-id)
FROM participated
WHERE participated.reportnumber=accident.reportnumber
AND accident.date = 2004
我正走在正确的轨道上吗?任何帮助将不胜感激。
答案 0 :(得分:5)
否,这是不对的,你必须引用accident
表:
SELECT COUNT(`driver-id`)
FROM participated
INNER JOIN accident ON participated.reportnumber = accident.reportnumber
WHERE accident.date = 2004
答案 1 :(得分:1)
您可以使用INNER JOIN
并加入事故表
SELECT COUNT(a.driver-id) as total
FROM participated a
LEFT JOIN accident b
ON a.reportnumber=b.reportnumber
WHERE b.date = 2004
答案 2 :(得分:0)
SELECT COUNT(DRIVER_ID) FROM person
WHERE DIVER_ID IN(SELECT DRIVER_ID FROM OWNER WHERE DRIVER_ID IN(SELECT CAR FROM participated WHERE REPORT_NUMBER IN(SELECT DATE FROM ACCIDENT WHERE DATE =2004);