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

时间:2022-07-27 11:45:04

I have a table named contacts:

我有一个名为contacts的表:

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

I had updated the values from these columns (Address, Address2, Address3) into:

我已将这些列(Address,Address2,Address3)中的值更新为:

       ID       combination
       1     No 4, Jalan, Mawar 
       2           NULL

by using this:

通过使用这个:

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

The problem is the updated value will be null if one the 3 columns is null.

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

2 个解决方案

#1


0  

alternatively you can use CONCAT_WS function. Detail information can be referred on W3Resource documentation here.

或者你可以使用CONCAT_WS功能。详细信息可在此处参考W3Resource文档。

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

#2


0  

You can query as below

您可以查询如下

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

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

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

更新联系人设置组合= CONCAT_WS(',',Address,Address2,Address3);

#1


0  

alternatively you can use CONCAT_WS function. Detail information can be referred on W3Resource documentation here.

或者你可以使用CONCAT_WS功能。详细信息可在此处参考W3Resource文档。

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

#2


0  

You can query as below

您可以查询如下

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

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

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

更新联系人设置组合= CONCAT_WS(',',Address,Address2,Address3);