简体   繁体   中英

concatenate two column values

I'd like to run a query on a table where I concatenate the the value of one column with another.

Something like the following (javascripty attempt):

UPDATE table
SET items = items+","+item1
WHERE item1 != "No Data" || item1 != "";

Is this possible to do in the database or do I need to be doing it in the middleware? Any pointers again much appreciated.

Take a look at concat_ws :

# Untested, but should work
UPDATE table
SET items = CONCAT_WS(',', items, item1)
WHERE item1 != "No Data" || item1 != "";

Another way to approach this problem:

UPDATE table
SET items = CONCAT(items,',',item1)
WHERE item1 != "No Data" OR item1 != "";

One of the main differences between CONCAT() and CONCAT_WS() function (as described by Mike above) is their handling of NULL values. For more information please see the String Functions section of the MySQL Reference Manual: http://dev.mysql.com/doc/refman/5.6/en/string-functions.html

There might be also some issues with using the "||"(pipes) characters if MySQL server has the PIPES_AS_CONCAT mode enabled. Just in case, it is better to change it into "OR": http://dev.mysql.com/doc/refman/4.1/en/server-sql-mode.html#sqlmode_pipes_as_concat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM