我想在 MySQL :
中进行以下查询SELECT DINSTINCT cstc.car_id FROM cars
JOIN customer_cars cstc ON cstc.age = student_cars.age
其中:
cars
表格包含以下列:car_id
,name
,...
customer_cars
表格包含以下列:car_id
,age
student_cars
表格包含以下列:car_id
,age
我知道查询语法错误,因为在此查询中无法识别student_cars
表(MySQL引发错误)。
(基本上,我想使用该查询找出 customer_cars 年龄等于 student_cars 年龄的所有车辆ID
那么,如何修改查询以使其正常工作?
答案 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