MySQL's Group_Concat function miss the nulls. How can group the rows including NULLs.

StackOverflow https://stackoverflow.com/questions/22411384

  •  14-06-2023
  •  | 
  •  

Domanda

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

È stato utile?

Soluzione

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;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top