[英]Mysql query based on date?
I have a table like this 我有这样的桌子
id | date | content
1 | 09-16-2013 | content 1 here
2 | 09-23-2013 | content 2 here
3 | 09-30-2013 | content 3 here
I would like to display the content for a week from that date. 我想显示从该日期开始一周的内容。 For example, the first content should start on 9/16/2013 and then show until 9/22/2013 mid night.
例如,第一个内容应从2013年9月16日开始,然后显示到2013年9月22日午夜。 then on next day, it changes to the content 2. Same way,when I am on content 2, I want to display like "previous week content" and then show just the previous ones..I think I can do this by checking the current date and then anything below that has to be displayed.
然后在第二天,它更改为内容2。以同样的方式,当我在内容2上时,我想要显示为“上周内容”,然后仅显示前一个内容。.我认为我可以通过检查当前日期,然后显示以下任何内容。
I am not very good at these kind of mysql queries, please advise! 我不太擅长这类mysql查询,请指教!
Regards 问候
I guess you're looking for something like this 我猜你在找这样的东西
SELECT *
FROM table1
WHERE date BETWEEN CURDATE() + INTERVAL 0 - WEEKDAY(CURDATE()) DAY
AND CURDATE() + INTERVAL 6 - WEEKDAY(CURDATE()) DAY
This query will grab a row(s) where date
column is within the boundaries of the current calendar week (from Monday to Sunday). 此查询将获取一行,其中
date
列在当前日历周的范围内(从星期一到星期日)。
WEEKDAY()
function returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday) . WEEKDAY()
函数返回日期的工作日索引(0 =星期一,1 =星期二,…6 =星期日) 。 The expression 表达方式
CURDATE() + INTERVAL 0 - WEEKDAY(CURDATE()) DAY
returns a date for Monday of the current calendar week and 返回当前日历周星期一的日期,并且
CURDATE() + INTERVAL 6 - WEEKDAY(CURDATE()) DAY
returns a date for Sunday of the current calendar week. 返回当前日历周的星期日的日期。 Using
BETWEEN
in WHERE
clause makes sure that a query returns only rows with date
values that falls between these two dates (Monday through Sunday). 在
WHERE
子句中使用BETWEEN
可以确保查询仅返回date
值介于这两个日期(星期一至星期日)之间的行。
Note: Make sure that you have an index on date
column. 注意:确保在
date
列上有索引。 This query is index-friendly. 该查询是索引友好的。
Sample output for today's date (09/19/2013): 今天日期(2013年9月19日)的示例输出:
+------+------------+----------------+ | id | date | content | +------+------------+----------------+ | 1 | 2013-09-16 | content 1 here | +------+------------+----------------+
UPDATE: To get records for previous calendar week you just substract 1 week interval from both values in BETWEEN
更新:要获取上一个日历周的记录,您只需从
BETWEEN
两个值中减去1周间隔
SELECT *
FROM table1
WHERE date
BETWEEN CURDATE() + INTERVAL 0 - WEEKDAY(CURDATE()) DAY - INTERVAL 1 WEEK,
AND CURDATE() + INTERVAL 6 - WEEKDAY(CURDATE()) DAY - INTERVAL 1 WEEK
尝试这个
SELECT * FROM table WHERE date BETWEEN '09-16-2013' AND '09-22-2013';
关键字是WEEK()
SELECT id,date, CONCAT('content ',WEEK(date),' to here') as content FROM table_name
SELECT *
FROM table
WHERE date BETWEEN '9/16/2013 00:00:00.00' AND '9/22/2013 00:00:00.00'
To select it dynamically, try something like 要动态选择它,请尝试类似
SELECT * FROM `yourTable` WHERE NOW() >= STR_TO_DATE(`date`, '%m-%d-%Y') ORDER BY STR_TO_DATE(`date`, '%m-%d-%Y') DESC LIMIT 1
or t 或t
SELECT * FROM `yourTable` WHERE CURDATE() >= STR_TO_DATE(`date`, '%m-%d-%Y') ORDER BY STR_TO_DATE(`date`, '%m-%d-%Y') DESC LIMIT 1
sqlfiddle example - http://sqlfiddle.com/#!2/62982/4 sqlfiddle例子- http://sqlfiddle.com/#!2/62982/4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.