MySQL - adding a word to the end of a text column

I need to add the word β€œexample” to the end of the keywords text column.

If there is already text in the column, the added word will be separated by a space :

 Column `keywords` = ''; Add word 'example' Result `keywords` = 'example' 

BUT

 Column `keywords` = 'Some text' Add word 'example' Result `keywords` = 'Some text example' 
+4
source share
6 answers
 UPDATE table SET keyword=( CASE WHEN keyword='' THEN 'example' ELSE concat(keyword,' example') END ); 
+7
source

to try

 UPDATE table SET `keyword` = CONCAT_WS(' ','your text',`keyword`) 

Reference

+2
source

Here is another approach that some may prefer:

 UPDATE `table` SET `keywords` = TRIM(CONCAT(`keywords`, ' ', 'example')) 

This one will not leave a leading space if the field is empty.

+1
source
 select concat(keyword,' example') from tbl ; 

Edition: To update, use below:

 UPDATE table SET keyword = CASE keyword WHEN '' THEN 'example' ELSE concat(keyword,' example') END; 
0
source

Try the following:

Select CONCAT (keywords, example) from myTable

0
source

try the following:

 UPDATE table SET `keyword` = CONCAT(`keyword`, ' ', 'example') 
0
source

All Articles