类似 SQL 的日期子句
我正在查询数据库以获取某年某月的所有博客文章。日期在数据库中存储为 YYYY-MM-DD
只获取特定日期和年份的帖子的最佳万无一失的方法是什么?我尝试过链接类似的子句,但我逐渐意识到它们会失败的几种方式,例如,如果我使用以下语句:
'SELECT * FROM entries WHERE date LIKE \''.$year.'%\' AND date LIKE \'%'.$month_no.'%\''
如果月份数字碰巧包含在年份的最后 3 位数字内,则这将失败数字。我是否必须使用正则表达式,如果是这样,有人可以建议我可以使用的语句吗?
I am querying a db to get all blog posts from a certain month of a certain year. The date is stored in the database as YYYY-MM-DD
What is the best foolproof method of getting only the posts of a certain day and year? I have tried chaining like clauses, but I've come to realise a few ways in which they would fail, for example if I used the statement:
'SELECT * FROM entries WHERE date LIKE \''.$year.'%\' AND date LIKE \'%'.$month_no.'%\''
This would fail if the month number happened to be contained within the last 3 digits of the year number. Would I have to use regex and if so, could anyone suggest a statement I could use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
请..仅使用日期过滤器。它将有助于提高性能(使用索引),
这适用于 MySQL。对于 SQL Server,使用类似的东西,但使用 DATEADD 代替。
添加一个月的原因是,当月份为 12(12 月)时更容易处理这种情况。
Please.. just use a date filter. It will help performance (use an index)
That was for MySQL. For SQL Server, use something similar, but use DATEADD instead
The reason for adding one month is that it is easier to handle the case when month is 12 (December).
我建议使用日期时间创建另一列(或者如果可能的话仅使用日期)。今后会容易得多。
或者,您可以将其转换为日期类型并进行查询。
I would recommend creating another column with datetime (or just date if possible). It would be much more easier going forward.
Alternatively, you can cast it to date type and query it.
我认为以下内容应该可以解决此问题:
但是,您需要使用任何脚本语言确保
$year
的长度为四位数字,而$month_no
的长度为两位数字等,您正在其中创建查询。I'd have thought the following should solve this:
However, you'd need to ensure that
$year
was four digits long and$month_no
was two digits using whatever scripting language, etc. you're creating the query in.如果DATE字段是DATE TYPE,那么“like”运算符不起作用,您必须将日期类型转换为varchar类型,然后您可以对转换结果使用“like”运算符。
再见
if DATE field is DATE TYPE then 'like' operator don't work, you have to transform date type in varchar type then you can use 'like' operator on result of trasformation.
Bye
您的数据作为字符串存储在数据库中而不是作为日期对象?如果可能的话,考虑使用
to_date
或CAST
从字符串创建日期对象。有很多日期时间函数使得这对于日期对象来说变得微不足道,例如 T-SQL 和 PL-SQL 中的DATEPART
或YEAR
、MONTH
在 MySql 中Your data is stored as a string in the database not as a date object? Look at creating a date object from the string if possible using
to_date
or aCAST
. There are plenty of date time functions which make this trivial for a date object e.g.DATEPART
in T-SQL and PL-SQL orYEAR
,MONTH
in MySql您没有说明您正在使用哪种类型的数据库。大多数产品都支持 DATE 数据类型,这对于列来说是显而易见的选择。
但假设您实际上使用的是字符列,那么您可以使用
substring()
或substr()
函数轻松解决它,而无需 REGEX:NB 抱歉,我不识别你的 SQL 方言,所以我可能弄错了精确的语法。
You don't say which flavour of database you're using. Most products support a DATE datatype and that would be the obvious choice for the column.
But presuming you are actually using a character column then you can easily solve it without REGEX by using a
substring()
orsubstr()
function:NB Sorry, I don't recognise your SQL dialect, so I may have got the precise syntax wrong.
万无一失的可能是将您的字符串转换为日期:
或者,如果您需要一个范围:
或者您可以转换两次:
CONVERT 中的数字 20 与您的日期格式匹配。
Foolproof would probably be to convert your strings to dates:
Or, if you need a range:
Or you could convert twice:
The number 20 in the CONVERT matches your date format.
当我制作受监控事件的数据库时,我遇到了这个问题(我的情况很复杂,因为就我所做的事情而言,我的日子实际上是从早上 8 点开始的,所以 01/02/2011 07:59:59” 做很多事情
实际上会算作 1 月 1 日!,您可以在某一天之后
将日期存储为包含时间的日期,然后。使用 Convert 将其转换为日期,您可以像平常一样查询(请参阅转换以了解转换)
按上述方式存储日期,并在其间使用,因此 $date1=01/01/2011 00:00:00 和 $date2=02/01/2011 00:00:00 会是
坚持你所拥有的和所做的
,但至少在完整的日期和时间下你仍然可以按时间顺序订购它们。
I ran into this when I made a database of monitored events (mine was complicated by the fact that in terms of what I was doing, my days actually ran from 8am in the morning, so 01/02/2011 07:59:59" actually would count as 1st Jan!, you can do a number of things
You were after a certain day. You didnt mention which language you're calling the SQL from so..
Store the date as dates inc times, and then use convert to convert it to just the date, which you can query as normal (see convert for converstions)
Store in dates as above, and use between, so $date1=01/01/2011 00:00:00 and $date2=02/01/2011 00:00:00 would be
stick with what you have and do
but at least with the full date and time you could still order them in order of the time.