Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.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 如果表中没有值,则查询应返回0_Php_Mysql - Fatal编程技术网

Php 如果表中没有值,则查询应返回0

Php 如果表中没有值,则查询应返回0,php,mysql,Php,Mysql,这是我的问题 select (max(booking_id),0) as bookingID from booking; 如果表中没有值,则查询应返回0。我怎样才能做到这一点呢?有两种选择: 使用IFNULL: select IFNULL(max(booking_id),0)) as bookingID from booking; 使用合并: select COALESCE(max(booking_id),0)) as bookingID from booking; 说明: IFNUL

这是我的问题

select (max(booking_id),0) as bookingID from booking;
如果表中没有值,则查询应返回0。我怎样才能做到这一点呢?

有两种选择:

  • 使用
    IFNULL

    select IFNULL(max(booking_id),0)) as bookingID from booking;
    
  • 使用
    合并

    select COALESCE(max(booking_id),0)) as bookingID from booking;
    
  • 说明:

    IFNULL
    只能在mysql中使用。但是,
    COALESCE
    几乎可以在所有RDBMS中使用。
    COALESCE
    返回列表中不为空的第一个参数。

    有两种选择:

  • 使用
    IFNULL

    select IFNULL(max(booking_id),0)) as bookingID from booking;
    
  • 使用
    合并

    select COALESCE(max(booking_id),0)) as bookingID from booking;
    
  • 说明:

    IFNULL
    只能在mysql中使用。但是,
    COALESCE
    几乎可以在所有RDBMS中使用。
    COALESCE
    返回列表中第一个不为null的参数。

    @KedarB:IFNULL仅用于mysql。但联合将几乎适用于所有RDBMS。@KedarB:IFNULL仅适用于mysql。但是,几乎所有RDBMS都可以使用联合。