2011-12-29 47 views
0

我願做下面的查詢中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的提高誤差)。

(基本上,我想用查詢,找出所有的汽車IDS其中customer_cars年齡等於student_cars歲)

所以,我怎麼能修改查詢,以使其正常工作?

回答

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

嘗試:

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