mysql 一对多 拼接关联id

一表对多表,需要查询在子表中主表数据关联的数量和主表关联的id
关键字:group_concat

SELECT pc_media.*,count(pc_media_car.Id) relate_cars,GROUP_CONCAT(DISTINCT pc_media_car.car_id) car_id  from pc_media 

LEFT JOIN pc_media_car ON pc_media.Id = pc_media_car.media_id

GROUP BY pc_media.Id
ORDER BY pc_media.Id desc

你可能感兴趣的:(mysql,数据库)