根据架构查找在MySQL中拥有汽车的总人数

时间:2013-05-20 15:18:19

标签: mysql

嘿伙计们我正在做一些考试修改,经过过去的论文,有人可以告诉我,如果我在正确的轨道上。

考虑下面给出的保险数据库:

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
我正走在正确的轨道上吗?任何帮助将不胜感激。

3 个答案:

答案 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);