Question

Consider the following example of Table1

usrID orderID deliverID
1     303     102
1     304     103
2     305     NULL
2     306     105
3     307     NULL
3     308     NULL

I want to concat the orderID, deliverID and group as usrID, as following

usrID orderID_group deliverID_group
1     303,304       102,103
2     305,306       NULL,105
3     307,308       NULL,NULL

I am using the following MySQL query which miss the NULL value.

SELECT usrID,
       GROUP_CONCAT(orderID SEPARATOR “,” ) AS orderID_group,
       GROUP_CONCAT(deliverID SEPARATOR “,”) AS deliverID_group
FROM   table1
GROUP  BY usrID;

I want to include both NOT NULL and NULL in sequence order. I would appreciate if anyone kindly write the MySQL query. Thanks

Was it helpful?

Solution

Try

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top