mysql GROUP_CONCAT(查询)

时间:2011-02-18 18:54:25

标签: mysql group-concat

我正在尝试运行看起来像这样的查询:

SELECT
  GROUP_CONCAT(vehicles.id)  
FROM                                                                
 vehicles                         
WHERE                                                               
 cars.id = vehicles.id

带来正确的数据:

1,2,3

但是,现在我想要使用GROUP_CONCAT

的品牌名称
SELECT
  GROUP_CONCAT(SELECT brand_name FROM brands WHERE vehicles.id = brand.id) 
FROM                                                                
 vehicles                         
WHERE                                                               
 vehicles.id = 100

所以我可以'Brand1', 'Brand2', 'Brand3'

1 个答案:

答案 0 :(得分:9)

SELECT
  GROUP_CONCAT(brands.brand_name) 
FROM                                                                
 vehicles
 inner join brands on vehicles.id=brands.id 
 inner join cars on cars.id=vehicles.id                       
WHERE                                                               
 cars.id = vehicles.id