CONCAT'ing NULL fields

前端 未结 11 988
小鲜肉
小鲜肉 2021-02-01 00:25

I have a table with three fields, FirstName, LastName and Email.

Here\'s some dummy data:

FirstName | LastName | Email
Adam        West       adam@west.c         


        
11条回答
  •  Happy的楠姐
    2021-02-01 00:55

    After observing the answers for this question, you may combine all of them into one simple solution

    CONCAT_WS(',',
    IF(NULLIF(FirstName, '') IS NULL, NULL, FirstName),
    IF(NULLIF(LastName, '') IS NULL, NULL, usr_lastname),
    IF(NULLIF(Email, '') IS NULL, NULL, Email))
    

    So, in short we use CONCAT_WS to concatenate our fields and separate them with ,; and notice that NULL fields nor EMPTY wont concatenated

    NULLIF will check if the field is NULL or EMPTY, a field that contains only spaces or is empty as well, ex: '', ' ') and the output will be either NULL or NOT NULL

    IF Will out put the field if it's not NULL or EMPTY

提交回复
热议问题