gpt4 book ai didi

如果任何字段包含 NULL,MySQL CONCAT 将返回 NULL

转载 作者:IT老高 更新时间:2023-10-28 12:49:47 24 4
gpt4 key购买 nike

我的表“设备”中有以下数据

affiliate_name  affiliate_location  model     ip             os_type    os_version 

cs1 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_name
FROM devices

它返回下面给出的结果

cs1-Dell-10.125.103.25-Linux-Fedora
cs2-Dell-10.125.103.26-Linux-Fedora
(NULL)
(NULL)

如何摆脱这种情况,以便它应该忽略 NULL 并且结果应该是

cs1-Dell-10.125.103.25-Linux-Fedora
cs2-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_name
FROM devices

关于如果任何字段包含 NULL,MySQL CONCAT 将返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15741314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com