简体   繁体   English

两周前选择查询

[英]Select query for two weeks ago

In my database table I have a field for date (varchar field to save date in yy-mm-dd format ), now I want to select records for two weeks ago. 在我的数据库表中,我有一个日期字段(varchar字段以yy-mm-dd格式保存日期),现在我想选择两周前的记录。 How can i do it ? 我该怎么做 ?

Implicit date arithmetic is fairly flexible in MySQL. 隐式日期算法在MySQL中相当灵活。 You can compare dates as strings without explicit use of CAST() or DATE() , but you're trusting MySQL to interpret your format correctly. 您可以将日期作为字符串进行比较,而无需明确使用CAST()DATE() ,但您信任MySQL以正确解释您的格式。 Luckily for you, it will do just fine with yy-mm-dd . 幸运的是,它对yy-mm-dd会很好。

I would recommend using BETWEEN and INTERVAL so that your query is easily readable; 我建议使用BETWEENINTERVAL以便您的查询易于阅读; for example: 例如:

SELECT * FROM Holidays 
WHERE Date BETWEEN (NOW() - INTERVAL 14 DAY) AND NOW();

The only trick with BETWEEN is that you have to put the lower bound first and the upper bound second; BETWEEN的唯一技巧是你必须将下限放在第一位,将上限放在第二位; for example, if you write BETWEEN 5 AND 2 , this always evaluates to FALSE because there is no value that can be greater than or equal to 5 while also being less than or equal to 2. 例如,如果你写BETWEEN 5 AND 2 ,则总是计算为FALSE因为没有值可以大于或等于5,同时也小于或等于2。

Here's a demo of the query in action at SQL Fiddle , and a list of the recognized INTERVAL expressions in MySQL. 下面是SQL Fiddle中运行的查询演示 ,以及MySQL中已识别的INTERVAL表达式列表。

Note that the parentheses around the expression NOW() - INTERVAL 14 DAY are not required but I would recommend using them here purely for the sake of clarity. 请注意,表达式NOW() - INTERVAL 14 DAY周围的括号不是必需的,但我建议在这里使用它们纯粹是为了清楚起见。 It makes the predicate clause just a little bit easier to read in the absence of proper syntax highlighting, at the expense of two characters. 它使谓词子句在没有正确语法突出显示的情况下更容易阅读,但代价是两个字符。

Ideally you should be using date types to store dates, but being that's not the case, you should look into casting to date then comparing. 理想情况下,你应该使用日期类型来存储日期,但事实并非如此,你应该考虑到目前为止的铸造然后进行比较。

select * from yourtable where cast (yourdate as Date) BETWEEN Date_Add(CURDATE(), INTERVAL -21 Day) and Date_Add(CURDATE(), INTERVAL -14 Day)

Note, this is untested and may need a little tweaking, but should give you a general idea of what you need to do. 请注意,这是未经测试的,可能需要稍微调整,但应该让您大致了解您需要做什么。

Also, if it's possible, you should really look into converting the varchar field to a date field....they have date types to prevent this sort of thing from happening, although i know changing field types isn't always a possibility. 此外,如果可能的话,你应该真正考虑将varchar字段转换为日期字段....它们有日期类型以防止这种事情发生,虽然我知道改变字段类型并不总是可能。

you can simply do with ADDDATE to get 14 days ago. 你可以简单地用ADDDATE做到14天前。 compare string with date will work. 比较字符串日期将起作用。

SELECT *
FROM your_table
WHERE your_date >= ADDDATE(NOW(), -14) AND your_date < NOW()

I use this for select data in past of past 我在过去的过去使用它来选择数据

SELECT * FROM Holidays 
WHERE a.dDate >= DATE( NOW( ) ) - INTERVAL 14
DAY AND a.dDate <= DATE( NOW( ) ) - INTERVAL 8

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

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