2015-12-08 121 views
0

我有2個表,汽車和經銷商。如何在連接兩個具有匹配ID的表時添加來自特定字段的列?

CARS: id, make, model, year, dealership_id 
DEALERSHIPS: id, name, address 

汽車有一個dealerhip_id屬性,但不是經銷商名稱。如果cars.dealership_id和dealership.id匹配,如何在加入表格時添加經銷商名稱?

這是我到目前爲止有:

SELECT cars.make, cars.model, cars.year 
FROM cars 
JOIN dealerships cars ON cars.dealership_id = dealership.id 

回答

1

這個怎麼樣?

SELECT cars.make, cars.model, cars.year, dealerships.name 
FROM cars 
JOIN dealerships ON cars.dealership_id = dealerships.id 
+0

謝謝,完美的工作 – cvDv

相關問題