MySQL CONCAT zwraca NULL, jeśli jakiekolwiek pole zawiera NULL

Mam następujące dane w tabeli "urządzenia"

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    

Wykonałem poniżej Zapytanie

SELECT CONCAT(`affiliate_name`,'-',`model`,'-',`ip`,'-',`os_type`,'-',`os_version`) AS device_name
FROM devices

Zwraca wynik podany poniżej

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

Jak z tego wyjść, żeby ignorowało NULL i wynik powinien być

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-
Author: John Woo, 2013-04-01

6 answers

Konwertuj wartości NULL z pustym łańcuchem, owijając je w COALESCE

SELECT CONCAT(COALESCE(`affiliate_name`,''),'-',COALESCE(`model`,''),'-',COALESCE(`ip`,''),'-',COALESCE(`os_type`,''),'-',COALESCE(`os_version`,'')) AS device_name
FROM devices
 181
Author: John Woo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-01 10:01:32

Użyj CONCAT_WS zamiast:

CONCAT_WS() nie pomija pustych łańcuchów. Jednak pomija wszelkie wartości NULL po argumencie separatora.

SELECT CONCAT_WS('-',`affiliate_name`,`model`,`ip`,`os_type`,`os_version`) AS device_name FROM devices
 100
Author: Gerry,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-30 17:13:01
SELECT CONCAT(isnull(`affiliate_name`,''),'-',isnull(`model`,''),'-',isnull(`ip`,''),'-',isnull(`os_type`,''),'-',isnull(`os_version`,'')) AS device_name
FROM devices
 10
Author: Harshil,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-01 10:15:32

Aby mieć taką samą elastyczność w CONCAT_WS jak w CONCAT (jeśli na przykład nie chcesz mieć tego samego separatora między każdym członem), użyj następującego wzoru:

SELECT CONCAT_WS("",affiliate_name,':',model,'-',ip,... etc)
 5
Author: patrick,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-05 13:04:19

CONCAT_WS nadal generuje dla mnie null, jeśli pierwsze pole jest Null. Rozwiązałem to dodając na początku ciąg o zerowej długości jak w CONCAT_WS("",affiliate_name,'-',model,'-',ip,'-',os_type,'-',os_version) jednak CONCAT("",affiliate_name,'-',model,'-',ip,'-',os_type,'-',os_version) generuje wartość Null, gdy pierwsze pole ma wartość Null.

 2
Author: Ken4Edge,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-27 00:04:18

Możesz użyć instrukcji if jak poniżej

select CONCAT(if(affiliate_name is null ,'',affiliate_name),'- ',if(model is null ,'',affiliate_name)) as model from devices
 1
Author: DNS,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-24 13:42:26