如何使我的查询工作(查询多个表)

时间:2011-12-29 11:44:36

标签: mysql sql database

我想在 MySQL

中进行以下查询
SELECT DINSTINCT cstc.car_id FROM cars
    JOIN customer_cars cstc ON cstc.age =  student_cars.age

其中:

cars表格包含以下列:car_idname,...

customer_cars表格包含以下列:car_idage

student_cars表格包含以下列:car_idage

我知道查询语法错误,因为在此查询中无法识别student_cars表(MySQL引发错误)。

(基本上,我想使用该查询找出 customer_cars 年龄等于 student_cars 年龄的所有车辆ID

那么,如何修改查询以使其正常工作?

2 个答案:

答案 0 :(得分:1)

如果你做对了,那就是它(未经证实):

select distinct c.car_id 
from cars c
inner join customer_cars cc on c.car_id = cc.car_id
inner join student_cars sc on c.car_id = sc.car_id
where cc.age = sc.age

答案 1 :(得分:1)

尝试:

SELECT DISTINCT c.car_id 
FROM customer_cars c
JOIN student_cars s ON c.car_id = s.car_id and c.age = s.age