存在空字段时的MySQL连接

存在空字段时的MySQL连接,mysql,sql,concat,Mysql,Sql,Concat,我使用以下代码创建了一个视图: CREATE OR REPLACE VIEW aaa AS SELECT pry.uid, treg.nombre_es as region, tpais.nombre_es as pais, tdep.departamento, dep_other, tciu.ciudad, ciu_other FROM tx_oriproyectos

我使用以下代码创建了一个视图:

CREATE OR REPLACE VIEW aaa AS

   SELECT pry.uid, 
          treg.nombre_es as region, 
          tpais.nombre_es as pais, 
          tdep.departamento, 
          dep_other, tciu.ciudad, 
          ciu_other
     FROM tx_oriproyectos_proyectos AS pry
LEFT JOIN tx_oritablascomunes_regiones as treg ON pry.region = treg.uid
LEFT JOIN tx_oritablascomunes_paises as tpais ON pry.pais = tpais.uid
LEFT JOIN tx_oritablascomunes_departamentos as tdep ON pry.departamento = tdep.uid
LEFT JOIN tx_oritablascomunes_ciudades as tciu ON pry.ciudad = tciu.uid
我得到了这个,还可以:

现在我需要获得如下连接结果:

concatenated_field
---------------------------------------
Africa - ALbania - Tirana1 - Tirana2
Africa - Colombia - Guaviare - Calamar
我该怎么做


我试过这个:

CREATE OR REPLACE VIEW aaa AS

SELECT CONCAT_WS (' - ', pry.uid, treg.nombre_es as region, tpais.nombre_es as pais, tdep.departamento, dep_other, tciu.ciudad, ciu_other)
FROM tx_oriproyectos_proyectos AS pry
LEFT JOIN tx_oritablascomunes_regiones as treg
ON pry.region=treg.uid
LEFT JOIN tx_oritablascomunes_paises as tpais
ON pry.pais=tpais.uid
LEFT JOIN tx_oritablascomunes_departamentos as tdep
ON pry.departamento=tdep.uid
LEFT JOIN tx_oritablascomunes_ciudades as tciu
ON pry.ciudad=tciu.uid
但我得到:

#1583 - Incorrect parameters in the call to native function 'CONCAT_WS'

好的,多亏@Mat,我终于得到了这段代码,这是这个问题的预期解决方案

CREATE OR REPLACE VIEW aaa AS

SELECT pry.uid, CONCAT_WS (' - ', treg.nombre_es, tpais.nombre_es, tdep.departamento, NULLIF(dep_other,''), tciu.ciudad, NULLIF(ciu_other,''))
FROM tx_oriproyectos_proyectos AS pry
LEFT JOIN tx_oritablascomunes_regiones as treg
ON pry.region=treg.uid
LEFT JOIN tx_oritablascomunes_paises as tpais
ON pry.pais=tpais.uid
LEFT JOIN tx_oritablascomunes_departamentos as tdep
ON pry.departamento=tdep.uid
LEFT JOIN tx_oritablascomunes_ciudades as tciu
ON pry.ciudad=tciu.uid

您应该能够使用string函数

SELECT CONCAT_WS(' - ', treg.nombre_es, tpais.nombre_es, ...) FROM ...
从文档中:

CONCAT_WS()不会跳过空字符串。但是,它会跳过分隔符参数之后的任何空值

因此,您的空值将被忽略

如果您还想跳过空字符串,可以在mix中使用throw
NULLIF
函数,如下所示:


您应该能够使用string函数

SELECT CONCAT_WS(' - ', treg.nombre_es, tpais.nombre_es, ...) FROM ...
从文档中:

CONCAT_WS()不会跳过空字符串。但是,它会跳过分隔符参数之后的任何空值

因此,您的空值将被忽略

如果您还想跳过空字符串,可以在mix中使用throw
NULLIF
函数,如下所示:


我使用concat尝试了不同的组合,但没有成功的结果。你在concat_ws调用中使用别名字段-这没有任何意义,因为一旦concat完成,字段本身将“消失”。我使用concat尝试了不同的组合,但没有成功的结果。您在concat_ws调用中使用了字段别名-这没有意义,因为一旦concat完成,字段本身将“消失”。或者使用
NULLIF()
函数:
选择CONCAT_WS('-',NULLIF(col',),…)
我一直在寻找它,但完全失败了,当时它就在与
CASE
语句相同的文档中。。。谢谢:)谢谢@Mat,我按照你的建议做了,但返回了错误。请参见上文。删除
作为name
部分,它们在该函数调用中毫无意义。现在它可以工作了!但我不知道为什么法典被修改了。请查看:或者使用
NULLIF()
函数:
选择CONCAT_WS('-',NULLIF(col',),…)
我正在查找它,但完全失败了,因为它与
CASE
语句位于同一文档中。。。谢谢:)谢谢@Mat,我按照你的建议做了,但返回了错误。请参见上文。删除
作为name
部分,它们在该函数调用中毫无意义。现在它可以工作了!但我不知道为什么法典被修改了。请看: