如果任何字段包含NULL,则MySQL CONCAT返回NULL
我的“设备”表中有以下数据
affiliate_name affiliate_location model ip os_type os_versioncs1 inter Dell 10.125.103.25 Linux Fedora
cs2 inter Dell 10.125.103.26 Linux Fedora
cs3 inter Dell 10.125.103.27 NULL NULL
cs4 inter Dell 10.125.103.28 NULL NULL
我执行以下查询
SELECT CONCAT(`affiliate_name`,'-',`model`,'-',`ip`,'-',`os_type`,'-',`os_version`) AS device_nameFROM devices
返回下面给出的结果
cs1-Dell-10.125.103.25-Linux-Fedoracs2-Dell-10.125.103.26-Linux-Fedora
(NULL)
(NULL)
如何得出这一点,以便它应该忽略NULL并且结果应该是
cs1-Dell-10.125.103.25-Linux-Fedoracs2-Dell-10.125.103.26-Linux-Fedora
cs3-Dell-10.125.103.27-
cs4-Dell-10.125.103.28-
回答:
NULL
通过将其包装在空字符串中来转换值COALESCE
SELECT CONCAT(COALESCE(`affiliate_name`,''),'-',COALESCE(`model`,''),'-',COALESCE(`ip`,''),'-',COALESCE(`os_type`,''),'-',COALESCE(`os_version`,'')) AS device_nameFROM devices
以上是 如果任何字段包含NULL,则MySQL CONCAT返回NULL 的全部内容, 来源链接: utcz.com/qa/414751.html