213

テーブル「デバイス」に次のデータがあります

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 AND 結果を無視するようにこれから抜け出す方法

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-
4

7 に答える 7

355

NULL値を空の文字列でラップして変換しますCOALESCE

SELECT CONCAT(COALESCE(`affiliate_name`,''),'-',COALESCE(`model`,''),'-',COALESCE(`ip`,''),'-',COALESCE(`os_type`,''),'-',COALESCE(`os_version`,'')) AS device_name
FROM devices
于 2013-04-01T10:01:32.447 に答える
14

CONCAT_WS で CONCAT と同じ柔軟性を持たせるには (たとえば、すべてのメンバー間で同じセパレーターを使用したくない場合)、次を使用します。

SELECT CONCAT_WS("",affiliate_name,':',model,'-',ip,... etc)
于 2015-01-05T13:04:19.323 に答える
10
SELECT CONCAT(isnull(`affiliate_name`,''),'-',isnull(`model`,''),'-',isnull(`ip`,''),'-',isnull(`os_type`,''),'-',isnull(`os_version`,'')) AS device_name
FROM devices
于 2013-04-01T10:15:32.870 に答える
3

以下のようなifステートメントを使用できます

select CONCAT(if(affiliate_name is null ,'',affiliate_name),'- ',if(model is null ,'',affiliate_name)) as model from devices
于 2015-09-24T13:42:26.603 に答える