如何在连接2个具有匹配ID的表时从某些字段添加列?

时间:2015-12-08 19:55:30

标签: mysql

我有2张桌子,汽车和经销商。

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

汽车有brokerhip_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 个答案:

答案 0 :(得分:1)

这个怎么样?

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