Linux MYSQL 检索日期 = 下周末的结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3677206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:25:55  来源:igfitidea点击:

MYSQL retrieve results where date = next weekend

phpmysqldate

提问by kalpaitch

Currently I have two tables and the following sql statement which correctly retrieves the items in the events table ordered by their dates in the event_dates table:

目前我有两个表和以下 sql 语句,它正确检索事件表中按 event_dates 表中的日期排序的项目:

SELECT * FROM events, event_dates 
WHERE events.id=event_dates.event_id 
     AND events.preview=0 AND event_dates.start_date>=now() 
ORDER BY event_dates.start_date ASC,event_dates.start_time ASC LIMIT 3

Now I want to add an extra AND to make sure only the events on the next weekend are set. The date column is in a standard mysql date format (YYYY-MM-DD). Got stuck on this bit. Cheers.

现在我想添加一个额外的 AND 以确保只设置下周末的事件。日期列采用标准的 mysql 日期格式 (YYYY-MM-DD)。卡在了这一点上。干杯。

回答by Spudley

Use PHP strtotime() to get the start and end timestamp of the weekend:

使用 PHP strtotime() 获取周末的开始和结束时间戳:

$we_start=strtotime('next saturday');
$we_end=strtotime('next monday')-1;

Then do a sql query to search for timestamps BETWEEN them.

然后执行 sql 查询以搜索它们之间的时间戳。

select * from mytable where UNIX_TIMESTAMP(mydatefield) BETWEEN $we_start AND $we_end

Hope that helps.

希望有帮助。