初学者的mySQL技巧

时间:2012-12-14 14:45:31

标签: mysql

PERSON
DriverID (primary)
DriverName
Address

CAR
RegNO (primary) 
Model 
Year

ACCIDENT
ReportNumber (primary)
AccidentDate
Location

OWNS
DriverID (primary key)  foreign key with PERSON(DriverID)
RegNo (primary Key) foreign key with CAR(RegNo)

PARTICIPATION
DriverID (primary key) foreign key with PERSON (DriverID)
RegNo (primary Key) foreign key with CAR(RegNo)
ReportNumber (primary key) foregin with ACCIDENT(primary)
Damage Amount

查询是 - >发现涉及特定车型的汽车事故的发生率

溶胶是

SELECT COUNT (a.ReportNumber)
FROM ACCIDENT a
WHERE
a.ReportNumber in ( SELECT pa.ReportNumber FROM PARTICIPATED pa, CAR c
WHERE pa.RegNo = c.RegNo AND c.Model = 'honda');

我的问题是如何理解涉及嵌套查询的复杂查询 在C ++中你可以使用断点来调试但是如何在mySQL中调试 我使用SQLyog,我知道包括GROUP在内的所有基本查询 但当它嵌套查询时,我无法理解

1 个答案:

答案 0 :(得分:0)

自行运行此查询:

SELECT pa.ReportNumber FROM PARTICIPATED pa, CAR c
WHERE pa.RegNo = c.RegNo AND c.Model = 'honda'

查看reportnumbers并将其置于脑海中。

现在运行此查询:

SELECT a.ReportNumber FROM ACCIDENT a

因此,第一个查询会在participated中使用'honda' car获取所有报告编号。第二个查询可以在reportnumber中为您提供所有accident。{/ p>

将它们组合在一起,您reportnumber accident participated 'honda'的所有{{1}}。{/ p>

我希望这能回答你的问题,我不清楚你在问什么。