mysql 仅选择下周数据

发布于 2024-11-19 12:13:31 字数 548 浏览 1 评论 0原文

我的数据库中有一个日期字段,即 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 的数据

I have a date field in my database i.e film_release_date.

So what is mysql query to fetch the next week data from table.

I run this query but its not working properlly.

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 

Actually its for movie website i have several movies in my database so i need a query which fetch every next week movie that means movies that will release in next week means next friday.If i run this today i.e 13 then it shows all movies between 15 to 22 and if run this between 15 to 22 then it shows data from 22 to 29

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云淡月浅 2024-11-26 12:13:31
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
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
半暖夏伤 2024-11-26 12:13:31

要获取下周的数据,您可以简单地应用以下逻辑:

SELECT something FROM tbl_name
    -> WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAYS)

参考

to get next week data you can simple apply below logic :

SELECT something FROM tbl_name
    -> WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAYS)

REFERENCE

折戟 2024-11-26 12:13:31

有点晚了,但没有一个答案能正常工作。您可以使用yearweek 函数来获取一年中的第几周。这样你就得到了 now(本周)的周数或者 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 

Little late, but none of the answers works properly. You can use yearweek function to get week number of the year. So you get the week number of now (this week) or now() + interval 7 day to get next week. Examples:

THIS WEEK

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 

NEXT WEEK

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文