组合两个查询,其中一个需要特定的SORT和LIMIT

时间:2012-05-14 12:11:06

标签: mysql

我有一个表,其中包含一个日期字段(schedule_date)和一个varchar字段(调度时间),其中包含一个小时数(0..23)作为字符串。

我有两个问题:

查询1)返回任何即将到来的时间表
a)任何有日期的东西>今天
b)以及日期与当前日期匹配但小时> 1的任何事物。当前时间

查询2)返回1条记录 - 上次运行的时间表

我一直在试图弄清楚如何将两者结合起来,但我不知道如何处理通过简单联合所涉及的两个问题。我猜测会涉及子查询,但我的SQL技能极限。

问题1):只有第二个查询需要“LIMIT 1” - 如何仅应用于查询2?

问题2):第二个查询需要DESC命令才能使LIMIT 1正常工作 但我需要两个查询的结果以ASC顺序组合

注意:在当前时间加上+ 1小时(商店日期在EST,服务器现在返回CST)

查询1:返回任何即将推出的时间表

SELECT schedule_date,  CONVERT(schedule_time, DECIMAL) AS schedule_time
FROM special_schedules
WHERE schedule_date > DATE(NOW())
UNION 
SELECT schedule_date,  CONVERT(schedule_time, DECIMAL) AS schedule_time
FROM special_schedules
WHERE schedule_date = DATE(NOW())
AND CONVERT(schedule_time, DECIMAL) > (HOUR(CURRENT_TIME()) + 1)

查询2:返回上一轮时间表 (需要DESC命令让LIMIT 1在最后的时间表上定位)

SELECT schedule_date,  CONVERT(schedule_time, DECIMAL) AS schedule_time
FROM special_schedules
WHERE schedule_date = DATE(NOW())
AND CONVERT(schedule_time, DECIMAL) < (HOUR(CURRENT_TIME()) + 1)
UNION
SELECT schedule_date,  CONVERT(schedule_time, DECIMAL) AS schedule_time
FROM special_schedules
WHERE schedule_date < DATE(NOW())
ORDER BY schedule_date DESC, schedule_time DESC
LIMIT 1

编辑:更正上面的ORDER(应该是DESC)

如果合并,需要将两者的结果排序组合成ASC:

ORDER BY schedule_date ASC, schedule_time ASC

谢谢!

2 个答案:

答案 0 :(得分:0)

SELECT schedule_date, schedule_time
FROM (( SELECT schedule_date, CONVERT(schedule_time, DECIMAL) AS schedule_time
        FROM special_schedules
        WHERE schedule_date = DATE(NOW())
            AND CONVERT(schedule_time, DECIMAL) < (HOUR(CURRENT_TIME()) + 1))
        UNION
    (   SELECT schedule_date, CONVERT(schedule_time, DECIMAL)
        FROM special_schedules
        WHERE schedule_date < DATE(NOW())
        ORDER BY schedule_date DESC
        LIMIT 1)) AS h
ORDER BY schedule_date DESC

这应该做你想要的。

答案 1 :(得分:0)

首先,您可以简单地UNION这两个查询,只需要将第二个查询括在括号中:

SELECT schedule_date,  CONVERT(schedule_time, DECIMAL) AS schedule_time
FROM special_schedules
WHERE schedule_date > DATE(NOW())
UNION 
SELECT schedule_date,  CONVERT(schedule_time, DECIMAL) AS schedule_time
FROM special_schedules
WHERE schedule_date = DATE(NOW())
AND CONVERT(schedule_time, DECIMAL) > (HOUR(CURRENT_TIME()) + 1)
UNION ALL
(
SELECT schedule_date,  CONVERT(schedule_time, DECIMAL) AS schedule_time
FROM special_schedules
WHERE schedule_date = DATE(NOW())
AND CONVERT(schedule_time, DECIMAL) < (HOUR(CURRENT_TIME()) + 1)
UNION
SELECT schedule_date,  CONVERT(schedule_time, DECIMAL) AS schedule_time
FROM special_schedules
WHERE schedule_date < DATE(NOW())
ORDER BY schedule_date DESC, schedule_time DESC
LIMIT 1
)
ORDER BY schedule_date ASC, schedule_time ASC

但您可以通过将UNION转换为使用OR条件的单个SELECT来简化两个查询,如下所示:

  1. SELECT schedule_date,  CONVERT(schedule_time, DECIMAL) AS schedule_time
    FROM special_schedules
    WHERE schedule_date > DATE(NOW())
       OR schedule_date = DATE(NOW()) AND CONVERT(schedule_time, DECIMAL) > HOUR(CURRENT_TIME())
    
  2. SELECT schedule_date,  CONVERT(schedule_time, DECIMAL) AS schedule_time
    FROM special_schedules
    WHERE schedule_date < DATE(NOW())
       OR schedule_date = DATE(NOW()) AND CONVERT(schedule_time, DECIMAL) < HOUR(CURRENT_TIME())
    ORDER BY schedule_date DESC, schedule_time DESC
    LIMIT 1
    
  3. 然后合并的查询将是:

    SELECT schedule_date,  CONVERT(schedule_time, DECIMAL) AS schedule_time
    FROM special_schedules
    WHERE schedule_date > DATE(NOW())
       OR schedule_date = DATE(NOW()) AND CONVERT(schedule_time, DECIMAL) > HOUR(CURRENT_TIME())
    UNION ALL
    (
    SELECT schedule_date,  CONVERT(schedule_time, DECIMAL) AS schedule_time
    FROM special_schedules
    WHERE schedule_date < DATE(NOW())
       OR schedule_date = DATE(NOW()) AND CONVERT(schedule_time, DECIMAL) < HOUR(CURRENT_TIME())
    ORDER BY schedule_date DESC, schedule_time DESC
    LIMIT 1
    )
    ORDER BY schedule_date ASC, schedule_time ASC
    

    还有另一个选项,它涉及将schedule_dateschedule_time转换为单个DATETIME - 值,如下所示:

    CONCAT(schedule_date, ' ', schedule_time, ':00')
    

    在这种情况下生成的查询可能如下所示:

    SELECT schedule_date,  CONVERT(schedule_time, DECIMAL) AS schedule_time
    FROM special_schedules
    WHERE CONCAT(schedule_date, ' ', schedule_time, ':00') > (NOW() + INTERVAL 1 HOUR)
    
    UNION ALL
    
    (
    SELECT schedule_date,  CONVERT(schedule_time, DECIMAL) AS schedule_time
    FROM special_schedules
    WHERE CONCAT(schedule_date, ' ', schedule_time, ':00') < (NOW() + INTERVAL 1 HOUR)
    ORDER BY schedule_date DESC, schedule_time DESC
    LIMIT 1
    )
    
    ORDER BY schedule_date ASC, schedule_time ASC