考虑以下Table1的例子
usrID orderID deliverID
1 303 102
1 304 103
2 305 NULL
2 306 105
3 307 NULL
3 308 NULL
我想将orderID,deliverID和group连接为usrID,如下所示
usrID orderID_group deliverID_group
1 303,304 102,103
2 305,306 NULL,105
3 307,308 NULL,NULL
我正在使用以下错误NULL值的MySQL查询。
SELECT usrID,
GROUP_CONCAT(orderID SEPARATOR “,” ) AS orderID_group,
GROUP_CONCAT(deliverID SEPARATOR “,”) AS deliverID_group
FROM table1
GROUP BY usrID;
我希望按顺序包含NOT NULL和NULL。如果有人友好地编写MySQL查询,我将不胜感激。感谢
答案 0 :(得分:2)
尝试
SELECT usrID,
GROUP_CONCAT(case when orderID is null then 'null' else orderID end order by orderID) AS orderID_group,
GROUP_CONCAT(case when deliverID is null then 'null' else deliverID end order by orderID) AS deliverID_group
FROM table1
GROUP BY usrID;