MySQL returning an empty field: CONCAT(nonEmpty1,empty2,nonEmpty3) = NULL

前端 未结 6 1138
攒了一身酷
攒了一身酷 2021-02-19 10:27

I have PHP 5 code accessing a MyISAM table on MySQL 5 server. The query looks like this:

SELECT CONCAT(fName1,\' \',mName2,\' \',lName3) AS userName 
    FROM us         


        
6条回答
  •  半阙折子戏
    2021-02-19 11:11

    In MySQL concatenating any string to a NULL value results in NULL. You have to check for NULL before concatenate using IFNULL:

    SELECT CONCAT(IFNULL(fName1,''),' ',IFNULL(mName2,''),' ',IFNULL(lName3,'')) AS userName 
    FROM users 
    WHERE level > 10
    

提交回复
热议问题