我的数据库中有一个日期字段,即film_release_date。
那么什么是mysql查询来从表中获取下一周的数据。
我运行此查询,但它无法正常工作。
SELECT
*,
DATE_FORMAT(film_release_date,'%e-%b-%Y') AS release_date
FROM gf_film
WHERE
MONTH(film_release_date)=MONTH(CURRENT_DATE)
AND YEAR(film_release_date)=YEAR(CURRENT_DATE)
AND film_release_date>=(CURRENT_DATE)
ORDER BY film_release_date DESC
实际上它的电影网站我在我的数据库中有几部电影,所以我需要一个查询,每周下载电影,这意味着下周将发布的电影意味着下周五。如果今天运行这个,即13,那么它显示所有电影在15到22之间,如果在15到22之间运行,那么它会显示22到29之间的数据
答案 0 :(得分:4)
set @d := date_add(CURRENT_DATE, interval 7 day);
set @week_start := @d - interval (dayofweek(@d) + 1) day;
set @week_end := @d + interval (6 - dayofweek(@d)) day;
SELECT *,
DATE_FORMAT(film_release_date,'%e-%b-%Y') AS release_date
FROM gf_film
WHERE film_release_date between @week_start and @week_end
ORDER BY film_release_date DESC
答案 1 :(得分:0)
要获得下周数据,您可以简单地应用于逻辑:
SELECT something FROM tbl_name
-> WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAYS)
答案 2 :(得分:0)
来晚了,但是所有答案都无法正常使用。您可以使用yearweek函数获取一年中的星期数。因此,您可以获得现在(本周)或now()的周数+间隔7天才能获得下周。例子:
本周
SELECT *, DATE_FORMAT(film_release_date,'%e-%b-%Y') AS release_date
FROM gf_film
WHERE YEARWEEK(film_release_date) = YEARWEEK(NOW())
ORDER BY film_release_date DESC
下一周
SELECT *, DATE_FORMAT(film_release_date,'%e-%b-%Y') AS release_date
FROM gf_film
WHERE YEARWEEK(film_release_date) = YEARWEEK(NOW() + INTERVAL 7 DAY)
ORDER BY film_release_date DESC