使用 CONCAT 显示空值将多列的值更新为一列
Update values from multiple columns into one using CONCAT display null value
我有一个 table 命名联系人:
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 列中的一列为空,更新后的值将为空。
或者您可以使用 CONCAT_WS 函数。详细信息可以参考 W3Resource 文档here。
update contacts set combination = CONCAT_WS (',', Address, Address2, Address3);
您可以如下查询
SELECT CONCAT_WS(',','1st string','2nd string', NULL);
update contacts set combination = CONCAT_WS(',',Address, Address2, Address3);
我有一个 table 命名联系人:
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 列中的一列为空,更新后的值将为空。
或者您可以使用 CONCAT_WS 函数。详细信息可以参考 W3Resource 文档here。
update contacts set combination = CONCAT_WS (',', Address, Address2, Address3);
您可以如下查询
SELECT CONCAT_WS(',','1st string','2nd string', NULL);
update contacts set combination = CONCAT_WS(',',Address, Address2, Address3);