Group_Concat in Concat not working with NULL values

后端 未结 3 1915
再見小時候
再見小時候 2021-01-12 05:54

I have a table

CREATE TABLE IF NOT EXISTS `dept` (
  `did` int(11) NOT NULL,
  `dname` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSE         


        
相关标签:
3条回答
  • 2021-01-12 06:18

    Hope following query will serve your purpose

    SELECT GROUP_CONCAT(
    IF(dname IS NOT NULL, CONCAT(did,"','",dname), CONCAT(did,"','NULL")) 
    SEPARATOR '),(') AS Result FROM dept
    
    0 讨论(0)
  • 2021-01-12 06:31

    try this, use COALESCE

    .., COALESCE(dname, 'NULL'),..
    

    making it NULL string visible. SQLFIDDLE DEMO

    0 讨论(0)
  • 2021-01-12 06:31

    From the MySQL aggregate function documentation:

    Unless otherwise stated, group functions ignore NULL values.

    Use COALESCE() to replace the nulls with a string, since they would be eliminated by the aggregate function. For example COALESCE(dbname, 'NULL') will return the string NULL if dbname IS NULL. Its purpose is to return the first non-null of the arguments you give it, and can therefore return a default value.

    SELECT
      GROUP_CONCAT(CONCAT(did,"','", COALESCE(dname, 'NULL')) SEPARATOR "'),('") AS Result
    FROM dept
    
    0 讨论(0)
提交回复
热议问题