1. 程式人生 > >mysql left join 逗號分隔欄位查詢!

mysql left join 逗號分隔欄位查詢!

商品表 goods 有學校欄位 school_ids

學校欄位 school_ids 中儲存學校表 school 的id,如果商品包含多個學校則多個id使用逗號分隔儲存

goods:


school:


查詢SQL:

select g.id,g.school_ids,GROUP_CONCAT(s.locality_name) from goods g left join school s on FIND_IN_SET(s.id , g.school_ids)
GROUP BY g.id
ORDER BY g.id ASC

結果: