0

I need some alternative of implode for mysql queries.After research found concat functions.

Question is, can I concat by whitespace like that CONCAT_WS(' ',sender.fname, sender.mname, sender.lname) AS sender_name ? is there any chance that it will give error if one of values is null?

Here is my query.

SELECT message.id, message.from_id, message.to_id, message.subject, 
                    message.date, message.deleted, message.read, 
                    CONCAT_WS(' ',sender.fname, sender.mname, sender.lname) AS sender_name, 
                    CONCAT_WS(' ',recipient.fname, recipient.mname, recipient.lname) AS recipient_name,  
                    FROM msghistory AS message 
                    LEFT JOIN users AS sender ON sender.id=message.from_id, 
                    LEFT JOIN users AS recipient ON recipient.id=message.to_id 
                    GROUP BY message.id DESC

One more question, in this query CONCAT_WS(' ',recipient.fname, recipient.mname, recipient.lname) AS recipient_name might not be found (if there is no matched row in users table). Will this give any error?

4 Answers 4

1

CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.

That should answer your question.

Sign up to request clarification or add additional context in comments.

2 Comments

One more question, in this query CONCAT_WS(' ',recipient.fname, recipient.mname, recipient.lname) AS recipient_name might not be found (if there is no matched row in users table). Will this give any error?
If there are not matched rows, the function won't be called. Simple as that.
1

Yes, you can use CONCAT_WS like that. If one of the arguments is NULL, there will be no error. The NULL argument is just not used.

It says this in the manual:

CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.

It's also very easy to verify this for yourself:

SELECT CONCAT_WS(' ', 'abc', NULL, 'def')
'abc def'

1 Comment

One more question, in this query CONCAT_WS(' ',recipient.fname, recipient.mname, recipient.lname) AS recipient_name might not be found (if there is no matched row in users table). Will this give any error?
1

Yes you can. If there will be null there wouldn't be any errors

6 Comments

is there any way to prevent this extra space?
I corrected my answer seconds before your comment, extra space will be if one of them will be just empty. If it's null they are skipped.
So SELECT CONCAT_WS(' ','sth','sth',null,'sth',null) will return sth sth sth but SELECT CONCAT_WS(' ','sth','sth','','sth','') will return sth sth sth I don't think there is way to prevent extra space in second example
One more question, in this query CONCAT_WS(' ',recipient.fname, recipient.mname, recipient.lname) AS recipient_name might not be found (if there is no matched row in users table). Will this give any error?
It shouldn't. You should just get empty result for that CONCAT_WS
|
0

Even though verbose, you need to use the COALESCE function when dealing with the potential for unacceptable NULL values:

CONCAT_WS(' ', COALESCE(val1, ''), COALESCE(val2, ''), ...)

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce

1 Comment

One more question, in this query CONCAT_WS(' ',recipient.fname, recipient.mname, recipient.lname) AS recipient_name might not be found (if there is no matched row in users table). Will this give any error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.