查询后,不要使用mySQL返回NULL行

时间:2018-01-04 13:16:06

标签: mysql

我有这个问题:

SELECT *,
CASE
    WHEN ___Bookings.BOO_DateCI = '2017-04-15' THEN "In Arrival"
    WHEN ___Bookings.BOO_DateCO = '2017-04-15' THEN "In Departure"
    WHEN '2017-04-15' > ___Bookings.BOO_DateCI AND '2017-04-15' < ___Bookings.BOO_DateCO THEN "In House"
END AS BOO_BookingStatus
FROM ___Bookings

我如何具体说明BOO_BookingStatus == NULL时我不想要该行?

感谢。

1 个答案:

答案 0 :(得分:0)

最简单的方法是将其包装在另一个查询中并定义条件:

SELECT *
FROM
    (SELECT *,
            CASE
                WHEN ___Bookings.BOO_DateCI = '2017-04-15' THEN "In Arrival"
                WHEN ___Bookings.BOO_DateCO = '2017-04-15' THEN "In Departure"
                WHEN '2017-04-15' > ___Bookings.BOO_DateCI
                     AND '2017-04-15' < ___Bookings.BOO_DateCO THEN "In House"
            END AS BOO_BookingStatus
     FROM ___Bookings) AS bookings_query
WHERE bookings_query.BOO_BookingStatus IS NOT NULL;

或明确排除您的CASE语句将返回null的所有情况:

SELECT *,
       CASE
           WHEN ___Bookings.BOO_DateCI = '2017-04-15' THEN "In Arrival"
           WHEN ___Bookings.BOO_DateCO = '2017-04-15' THEN "In Departure"
           WHEN '2017-04-15' > ___Bookings.BOO_DateCI
                AND '2017-04-15' < ___Bookings.BOO_DateCO THEN "In House"
       END AS BOO_BookingStatus
FROM ___Bookings
WHERE ___Bookings.BOO_DateCI = '2017-04-15'
    OR ___Bookings.BOO_DateCO = '2017-04-15'
    OR ('2017-04-15' > ___Bookings.BOO_DateCI   
        AND '2017-04-15' < ___Bookings.BOO_DateCO)