过年mysql什么意思,MySQL周数和新年

The website I'm working on now has a dashboard showing data entered during the previous week by various users. I select this data using a simple WHERE clause:

SELECT * FROM table WHERE WEEK(date, 1) = WEEK(CURDATE(), 1) - 1

However, the New Year is coming soon, and when a user tries to view the dashboard on, for example, 3rd or 4th of January, my code is clearly going to give me a wrong result, because the last week's number of 2010 is 52. So, what would be the best way to alter my query to take into account the change of the year. And also, it would be cool to make it possible to select data entered 2, 3, 4,... weeks ago.

解决方案

How about selecting the WEEK of the day seven days ago?

WHERE WEEK(date, 1) = WEEK(CURDATE() - INTERVAL 1 WEEK, 1)

This way you can select data entered 2,3,4 weeks ago:

WHERE WEEK(date, 1) = WEEK(CURDATE() - INTERVAL 2 WEEK, 1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值