3

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

我有两个查询:

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

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

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

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

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

注意:将 + 1 小时添加到下面的当前时间(存储日期为 EST,服务器现在返回 CST)

QUERY 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)

QUERY 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

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

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

ORDER BY schedule_date ASC, schedule_time ASC

谢谢你!

4

2 回答 2

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

这应该做你所追求的。

于 2012-05-14T12:36:20.743 回答
0

首先,您可以简单地将两个查询联合起来,只有第二个需要用括号括起来:

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-ed 条件的单个 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
    

然后组合查询将是这样的:

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
于 2012-05-14T15:13:10.907 回答