Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Mysql。如何在不合并的情况下用resultset中的特定预定义值替换NULL_Php_Mysql - Fatal编程技术网

Php Mysql。如何在不合并的情况下用resultset中的特定预定义值替换NULL

Php Mysql。如何在不合并的情况下用resultset中的特定预定义值替换NULL,php,mysql,Php,Mysql,我有这样的疑问: SELECT table1.field1, table1.field2, table2.field3 FROM table1 LEFT JOIN table2 ON (...) 由于左连接,有时表2.field3为空 在循环中的php脚本中,我手动重新定义了它 ... $row['field3'] = ($row['field3'] === null) ? 'undefined' : $row['field3']; ... 但是我如何才能在我的结果集中得到这个“未定义的”

我有这样的疑问:

SELECT table1.field1, table1.field2, table2.field3
FROM table1
LEFT JOIN table2 ON (...)
由于左连接,有时表2.field3为空

在循环中的php脚本中,我手动重新定义了它

...
$row['field3'] = ($row['field3'] === null) ? 'undefined' : $row['field3']; 
...
但是我如何才能在我的结果集中得到这个“未定义的”

我一直在寻找答案,联合似乎是有效的,但它有一些奇怪的逻辑,有许多论点。 看起来它能做我想做的,但有点奇怪


我很感兴趣的是,是否有另一种简单的方法将我的NULL替换为“未定义”?

您不应该害怕合并。。。没关系

但是,还有其他方法:

  • 只要使用IF&IS NULL

    如果(table2.field3为空,'undefined',table2.field3)为字段3,则选择table1.field1、table1.field2

  • 或者更简单,使用IFNULL(我相信您会喜欢它)

    选择table1.field1、table1.field2、IFNULL(table2.field3,“未定义”)作为字段3


  • 你不应该害怕联合。。。没关系

    但是,还有其他方法:

  • 只要使用IF&IS NULL

    如果(table2.field3为空,'undefined',table2.field3)为字段3,则选择table1.field1、table1.field2

  • 或者更简单,使用IFNULL(我相信您会喜欢它)

    选择table1.field1、table1.field2、IFNULL(table2.field3,“未定义”)作为字段3


  • 您也可以在

    Select 
        case when table1.field1 is null then 
            case when table1.field2 is null then 'undefined'
            else table1.field2 end 
        else table1.field1 end as field1
    from tablename
    

    如果要检查进一步的级别条件,可以使用嵌套大小写。

    如果

    Select 
        case when table1.field1 is null then 
            case when table1.field2 is null then 'undefined'
            else table1.field2 end 
        else table1.field1 end as field1
    from tablename
    

    当您想进一步检查级别条件时,可以使用嵌套大小写。

    您看过IFNULL吗?蒂姆·米奇,没有。谢谢你看了IFNULL?tim Mickey没有。谢谢这比我想象的要简单。谢谢这比我想象的要简单。谢谢你