I have a table with three fields, FirstName, LastName and Email.
Here\'s some dummy data:
FirstName | LastName | Email
Adam West adam@west.c
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