简体   繁体   English

MySQL选择取决于一天中的时间

[英]MySQL select dependant on time of day

I want to select from a MySQL table and filter depending on the time of day: 我想从MySQL表中选择并根据一天中的时间进行过滤:

if now > 10am select uploaded date where created date is today

if now < 10am select uploaded date where created date is yesterday

I think you must use Cron jobs for this. 我认为您必须为此使用Cron作业

Then, your sql will look like that: 然后,您的sql将如下所示:

WHERE tbl.TimeCreated = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)

assuming this format of the field: DateTime(YYYY-MM-DD HH:MM:SS) 假定该字段的格式为: DateTime(YYYY-MM-DD HH:MM:SS)

This should do what you need. 这应该做您需要的。

SELECT
    `uploaded_date`
FROM
    `table`
WHERE
    `created_date` = CASE WHEN CURTIME() > 100000
                       THEN CURDATE()
                       ELSE CURDATE() - INTERVAL 1 DAY
                     END

You can see a similar example in this fiddle which should return a single row of "AM" in the morning, "Noon" at noon, and "PM" in the afternoon (and 2 empty result sets). 您可以在此提琴中看到一个类似的示例, 示例应在早上返回一行“ AM”,在中午返回“ Noon”,在下午返回“ PM”(以及两个空结果集)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM