JOIN中的CONCAT_WS行

时间:2013-06-28 21:56:52

标签: mysql sql

我有桌子

profiles (id, name, deleted)
categories (id, name, deleted)
profiles_categories (id, profile_id, category_id, , deleted)

我有错误的查询

SELECT p.id, p.name CONCAT_WS(', ', c.name) AS keywords_categories
FROM profiles p
LEFT JOIN profiles_categories pc ON p.id = pc.profile_id
LEFT JOIN categories c ON pc.id = c.id
WHERE p.deleted = FALSE 

所以,我想要使用带有concan profiles所有 categories.name获得结果。

由于

1 个答案:

答案 0 :(得分:4)

目前还不完全清楚但似乎您确实想要使用GROUP_CONCAT,这会为每个配置文件创建一个以逗号分隔的所有类别列表:

SELECT p.id, 
  p.name, 
  GROUP_CONCAT(c.name SEPARATOR ', ') AS keywords_categories
FROM profiles p
LEFT JOIN profiles_categories pc ON p.id = pc.profile_id
LEFT JOIN categories c ON pc.id = c.id
WHERE p.deleted = FALSE 
GROUP BY p.id, p.name