1

我有桌子

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得到 结果。profilescategories.name

谢谢

4

1 回答 1

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
于 2013-06-28T22:04:29.987 回答