mysql concate check not not null

Bha*_*mar 13 mysql

SELECT Concat(mp.cHospital ,',',mp.cHospital1,',',mp.cHospital2) as Hospital FROM TBL
Run Code Online (Sandbox Code Playgroud)

我不想返回null值,

如何检查不为null或如何在查询中使isset cond

joh*_*nes 27

根据定义(几乎)任何具有NULL的操作都将导致NULL,因为NULL表示"未定义".我解释你要检查的cHospital或cHospital1或cHospital3可能为NULL的问题.问题是:应该怎么办?你想要用空的搅拌器取代现场,然后是concat或者全部?

我假设第一个.可能看起来像这样:

SELECT Concat(
    IFNULL(mp.cHospital, ''),
    ',',
    IFNULL(mp.cHospital1,''),
    ',',
    IFNULL(mp.cHospital2,'')) AS Hospital
FROM TBL
Run Code Online (Sandbox Code Playgroud)

IFNULL返回第一部分,除非它返回第二部分的NULL(这里是空字符串).

http://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html#function_ifnull

  • 我不知道你要什么 (2认同)

小智 12

我建议使用CONCAT_WS功能.这可以根据您的需要进行操作.会是这样的

SELECT CONCAT_WS(',', mp.cHospital, mp.cHospital1, mp.cHospital2) as Hospital FROM TB大号

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat-ws

问候!!