简体   繁体   English

获取两周之间的所有记录

[英]Get all records between two weeks

I need to get all records between two week numbers. 我需要获取两周之间的所有记录。 This is the query I'm using right now: 这是我现在正在使用的查询:

SELECT huxwz_user_orders . * , huxwz_users.name, huxwz_users.email FROM huxwz_user_orders
LEFT OUTER JOIN huxwz_users ON ( huxwz_user_orders.userid = huxwz_users.id )
WHERE
   (STATUS=3 or STATUS=2)
    AND plannedweek > 0 
    AND plannedweek >= WEEK(DATE_ADD(now(), interval 1 WEEK))
    AND plannedweek < WEEK(DATE_ADD(now(), interval 3 WEEK))
    AND NOT plannedweek=0

The query works very well and returns all records between the two provided weeks. 该查询工作得很好,并返回了所提供的两个星期之间的所有记录。 HOWEVER When I add like 16 to the < ending of the query. 但是,当我在查询的<结尾添加16时。 MySQL interprets the 16 weeks should be added to the current week (42), So it's 16+42 = 58. Since we dont have 58 weeks and only 52, it must be the sixth week. MySQL解释说应该将16周添加到当前周(42),所以它是16 + 42 =58。由于我们没有58周而只有52周,所以它必须是第六周。 This is exactly how it should do. 这正是它应该做的。

Problem is, when I want to find all records between two values and it does like above. 问题是,当我想查找两个值之间的所有记录时,它确实如上。 It's going to look like this: 它看起来像这样:

Get all records that are > 42 and < 16. This makes no sense, and it will obviously not return anything. 获取所有大于42和小于16的记录。这没有任何意义,并且显然不会返回任何内容。

So, my records have a year, so I do know what year it is. 因此,我的记录有一年,所以我确实知道它是哪一年。 I was thinking something along the lines: If the week, is next year, add 52 to the value. 我一直在想一些事情:如果是明年,则将值加52。 This is unreliable though, since not every year has 52 weeks. 但是,这并不可靠,因为并非每年都有52周。

How would I do this? 我该怎么做?

Any suggestions / solutions? 有什么建议/解决方案吗?

It looks like @Strawberry's comment is on the right track. 看来@Strawberry的评论正确无误。

I corrected Strawberry's omission of the DATE_ADD() function, plus we need to adapt the value of planned week to match the results of the YEARWEEK() function. 我更正了Strawberry对DATE_ADD()函数的遗漏,此外,我们还需要调整计划周的值以匹配YEARWEEK()函数的结果。

eg: 例如:

SELECT huxwz_user_orders . * , huxwz_users.name, huxwz_users.email FROM huxwz_user_orders
LEFT OUTER JOIN huxwz_users ON ( huxwz_user_orders.userid = huxwz_users.id )
WHERE
   (STATUS=3 or STATUS=2)
    AND plannedweek > 0 
    AND (YEAR( NOW() ) * 100) + plannedweek >= YEARWEEK( WEEK(DATE_ADD(now(), interval 1 WEEK)), 1)
    AND (YEAR( NOW() ) * 100) + plannedweek < YEARWEEK( WEEK(DATE_ADD(now(), interval 3 WEEK)), 1) 
    AND NOT plannedweek=0                  

If this doesn't work then your question may need some further clarification. 如果这不起作用,那么您的问题可能需要进一步澄清。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM