Concat分组记录

时间:2015-04-12 19:28:51

标签: mysql

我有两个相关的表,“project”和“projects_formats”by projectID。

我需要一个可以为我提供项目的查询,并且每行我想要连接这个项目所需的格式集。

现在,我的查询是:

SELECT SQL_CALC_FOUND_ROWS
       f.*,
       CONCAT(f.formatID, ', ') as formats
FROM projects as p INNER JOIN project_formats as f ON f.projectID=p.ID 
WHERE p.ended=0
GROUP BY f.projectID

这可能吗?

1 个答案:

答案 0 :(得分:1)

而不是CONCAT,请尝试GROUP_CONCAT

SELECT SQL_CALC_FOUND_ROWS
       f.*,
       GROUP_CONCAT(f.formatID SEPARATOR ', ') as formats
FROM projects as p INNER JOIN project_formats as f ON f.projectID=p.ID 
WHERE p.ended=0
GROUP BY f.projectID