Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an extra comma ',' while using concat

Tags:

mysql

I have wrote a stored proc in sqlyog. It is quite long and performing all the desired functionality except for the concat statement so I will only list that particular query.

UPDATE recipes_new 
SET co_auths = CONCAT(co_auths,',',c1id) 
WHERE id = name_in; 

I basically want a separation in the two fields and this statement is placed in a cursor so it is iterative. co_auths is null at the moment so I get the result as ,1,2,3 where as I want it to be 1,2,3. Any guesses what can the most appropriate solution be?

like image 442
Shahzaib Avatar asked Feb 22 '13 14:02

Shahzaib


1 Answers

By using an IF:

UPDATE recipes_new 
SET co_auths = IF(co_auths IS NULL, c1id, CONCAT(co_auths, ',', c1id))
WHERE id = name_in; 

If the value of co_auths is an empty string instead of NULL:

UPDATE recipes_new 
SET co_auths = IF(LENGTH(co_auths), CONCAT(co_auths, ',', c1id), c1id)
WHERE id = name_in; 
like image 168
Ja͢ck Avatar answered Sep 23 '22 21:09

Ja͢ck