使用CONCAT显示空值将多列中的值更新为一列

时间:2015-09-03 08:11:45

标签: mysql database phpmyadmin

我有一个名为contacts的表:

   ID  Address  Address2    Address3
    1    No 4     Jalan      Mawar
    2    No 1     Street 2   NULL

我已将这些列(地址,地址2,地址3)中的值更新为:

       ID       combination
       1     No 4, Jalan, Mawar 
       2           NULL

使用这个:

  update contacts set combination = concat(Address, ', ', Address2, ', ', Address3);

问题是如果3列为空,则更新后的值为null。

2 个答案:

答案 0 :(得分:0)

或者您可以使用CONCAT_WS功能。详细信息可参考W3Resource文档here

update contacts set combination = CONCAT_WS (',', Address, Address2, Address3);

答案 1 :(得分:0)

您可以按以下方式查询

SELECT CONCAT_WS(',','1st string','2nd string', NULL);

update contacts set combination = CONCAT_WS(',',Address, Address2, Address3);