MYSQL:使用group_concat左连接并将外观限制为一个

时间:2014-04-11 06:42:12

标签: mysql sql

我有一个像这样的SQL:

SELECT TASKID, TSK_STATUSID, TSK_CODE, TSK_TITLE, TSK_REF_NO, TSK_DESCRIPTION, STS_NAME, COALESCE(USR_NAME, 'None') AS TUSERNAME 
FROM T_TASK_REQUEST 
LEFT JOIN T_STATUS ON T_STATUS.STATUSID=T_TASK_REQUEST.TSK_STATUSID 
LEFT JOIN T_TASK_USER ON TUS_TASKID = TASKID 
LEFT JOIN T_USER ON USERID = TUS_USERID 
LEFT JOIN T_PROJECT ON T_PROJECT.PROJECTID=T_TASK_REQUEST.PROJECTID 
WHERE TSK_STATUSID='1' AND T_TASK_REQUEST.PROJECTID='2' ORDER BY TASKID DESC LIMIT 10   

目前的结果就像这个enter image description here

如何将 TASKID 分组为1行, TUSERNAME 结果为:boon | danny | jack | goh | ncc

分开 |

在此处查看完整尺寸的IMG:http://i.stack.imgur.com/WXH3g.jpg

1 个答案:

答案 0 :(得分:0)

修改后的确切工作SQL是:

SELECT TASKID, TSK_STATUSID, TSK_CODE, TSK_TITLE, TSK_REF_NO, TSK_DESCRIPTION, STS_NAME, GROUP_CONCAT(DISTINCT COALESCE(USR_NAME, 'None') SEPARATOR '|') AS TUSERNAME 
FROM T_TASK_REQUEST 
LEFT JOIN T_STATUS ON T_STATUS.STATUSID=T_TASK_REQUEST.TSK_STATUSID 
LEFT JOIN T_TASK_USER ON TUS_TASKID = TASKID 
LEFT JOIN T_USER ON USERID = TUS_USERID 
LEFT JOIN T_PROJECT ON T_PROJECT.PROJECTID=T_TASK_REQUEST.PROJECTID 
WHERE TSK_STATUSID='1' AND T_TASK_REQUEST.PROJECTID='2' 
GROUP BY TASKID
ORDER BY TASKID DESC LIMIT 10   

感谢@John Woo