MYSQL SELECT如果选择ELSE如果选择查询

时间:2014-01-25 12:28:35

标签: mysql

MYSQL DATE RANGE and TIME RANGE

我前一段时间问这个问题我需要帮助mysql SELECT IF SELECT ELSE IF在我的查询中有关获取Graveyard shift的完整和待定日志表,其中有一个范围在今天晚上10点到TOM 7AM。这可能吗?有什么建议吗?它将计算挂起的日志表的数量

SELECT count(*) FROM faxlogsheet 
    IF 
   (date = CURDATE() and (time_received >='22:00' and time_received <= '23:59')
    THEN 
        SELECT * FROM faxlogsheet
             WHERE ((date = CURDATE() and (time_received >='22:00' and time_received <= '23:59')) 
                 or (date=CURDATE() + INTERVAL 1 DAY and (time_received >= '00:00' and time_received <= '7:00'))) 
             and shift = 'GY' and complete = 0
        ELSE IF
            (date = CURDATE() and (time_received >= '00:00' and time_received <= '7:00')
           THEN
             SELECT * FROM faxlogsheet
                WHERE ((date = CURDATE() and (time_received >= '00:00' and time_received <= '7:00')) 
                   or (date=CURDATE() - INTERVAL 1 DAY and (time_received >='22:00' and time_received <= '23:59'))) 
                   and shift = 'GY' and complete = 0

1 个答案:

答案 0 :(得分:0)

它们完全是两个不同的查询,你应该在这里使用2个查询,因为你无法在IF子句中使用select statment。

第一次查询

SELECT * FROM faxlogsheet
                     WHERE ((date = CURDATE() and (time_received >='22:00' and time_received <= '23:59')) 
                       or (date=CURDATE() + INTERVAL 1 DAY and (time_received >= '00:00' and time_received <= '7:00'))) 
                     and shift = 'GY' and complete = 0

第二次查询

SELECT * FROM faxlogsheet
                        WHERE ((date = CURDATE() and (time_received >= '00:00' and time_received <= '7:00')) 
                         or (date=CURDATE() - INTERVAL 1 DAY and (time_received >='22:00' and time_received <= '23:59'))) 
                         and shift = 'GY' and complete = 0");