类似 SQL 的日期子句

发布于 2024-10-28 12:29:10 字数 321 浏览 1 评论 0原文

我正在查询数据库以获取某年某月的所有博客文章。日期在数据库中存储为 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 技术交流群。

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

发布评论

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

评论(8

紫罗兰の梦幻 2024-11-04 12:29:10

请..仅使用日期过滤器。它将有助于提高性能(使用索引),

$query = "SELECT *
FROM entries
WHERE date >= '" . $year . "-" . $month_no . "-01'
  AND date <  adddate('" . $year . "-" . $month_no . "-01', interval 1 month)";

这适用于 MySQL。对于 SQL Server,使用类似的东西,但使用 DATEADD 代替。

...
WHERE date >= '$year" . $month . "01'
  AND date <  DATEADD(month,1,'$year" . $month . "01'";

添加一个月的原因是,当月份为 12(12 月)时更容易处理这种情况。

Please.. just use a date filter. It will help performance (use an index)

$query = "SELECT *
FROM entries
WHERE date >= '" . $year . "-" . $month_no . "-01'
  AND date <  adddate('" . $year . "-" . $month_no . "-01', interval 1 month)";

That was for MySQL. For SQL Server, use something similar, but use DATEADD instead

...
WHERE date >= '$year" . $month . "01'
  AND date <  DATEADD(month,1,'$year" . $month . "01'";

The reason for adding one month is that it is easier to handle the case when month is 12 (December).

蛮可爱 2024-11-04 12:29:10

我建议使用日期时间创建另一列(或者如果可能的话仅使用日期)。今后会容易得多。

或者,您可以将其转换为日期类型并进行查询。

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.

烙印 2024-11-04 12:29:10

我认为以下内容应该可以解决此问题:

'SELECT * FROM entries WHERE date LIKE \''.$year.'-'.$month_no.'-%\''

但是,您需要使用任何脚本语言确保 $year 的长度为四位数字,而 $month_no 的长度为两位数字等,您正在其中创建查询。

I'd have thought the following should solve this:

'SELECT * FROM entries WHERE date LIKE \''.$year.'-'.$month_no.'-%\''

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.

故乡的云 2024-11-04 12:29:10

如果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

辞别 2024-11-04 12:29:10

您的数据作为字符串存储在数据库中而不是作为日期对象?如果可能的话,考虑使用 to_dateCAST 从字符串创建日期对象。有很多日期时间函数使得这对于日期对象来说变得微不足道,例如 T-SQL 和 PL-SQL 中的 DATEPARTYEARMONTH在 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 a CAST. There are plenty of date time functions which make this trivial for a date object e.g. DATEPART in T-SQL and PL-SQL or YEAR, MONTH in MySql

残花月 2024-11-04 12:29:10

您没有说明您正在使用哪种类型的数据库。大多数产品都支持 DATE 数据类型,这对于列来说是显而易见的选择。

但假设您实际上使用的是字符列,那么您可以使用 substring()substr() 函数轻松解决它,而无需 REGEX:

'SELECT * FROM entries 
WHERE substr(date, 1, 7) =  '.$year.'-'.$month_no

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() or substr() function:

'SELECT * FROM entries 
WHERE substr(date, 1, 7) =  '.$year.'-'.$month_no

NB Sorry, I don't recognise your SQL dialect, so I may have got the precise syntax wrong.

狼性发作 2024-11-04 12:29:10

万无一失的可能是将您的字符串转换为日期:

DECLARE @dateToFind datetime

SET @dateToFind = CONVERT(datetime, N'2011-04-02', 20)

SELECT * FROM [table] WHERE CONVERT(datetime, [datefield], 20) = @dateToFind

或者,如果您需要一个范围:

DECLARE @startDate datetime, @endDate datetime

SELECT 
    @startDate = CONVERT(datetime, N'2011-04-01', 20), 
    @endDate = CONVERT(datetime, N'2011-04-30', 20) 

SELECT * FROM [table] WHERE CONVERT(datetime, [datefield], 20) BETWEEN @startDate AND @endDate

或者您可以转换两次:

SELECT * FROM [table] WHERE YEAR(CONVERT(datetime, [datefield], 20)) = 2011 AND MONTH(CONVERT(datetime, [datefield], 20)) = 4

CONVERT 中的数字 20 与您的日期格式匹配。

Foolproof would probably be to convert your strings to dates:

DECLARE @dateToFind datetime

SET @dateToFind = CONVERT(datetime, N'2011-04-02', 20)

SELECT * FROM [table] WHERE CONVERT(datetime, [datefield], 20) = @dateToFind

Or, if you need a range:

DECLARE @startDate datetime, @endDate datetime

SELECT 
    @startDate = CONVERT(datetime, N'2011-04-01', 20), 
    @endDate = CONVERT(datetime, N'2011-04-30', 20) 

SELECT * FROM [table] WHERE CONVERT(datetime, [datefield], 20) BETWEEN @startDate AND @endDate

Or you could convert twice:

SELECT * FROM [table] WHERE YEAR(CONVERT(datetime, [datefield], 20)) = 2011 AND MONTH(CONVERT(datetime, [datefield], 20)) = 4

The number 20 in the CONVERT matches your date format.

辞别 2024-11-04 12:29:10

当我制作受监控事件的数据库时,我遇到了这个问题(我的情况很复杂,因为就我所做的事情而言,我的日子实际上是从早上 8 点开始的,所以 01/02/2011 07:59:59” 做很多事情

实际上会算作 1 月 1 日!,您可以在某一天之后

  1. ,您没有提到您从哪种语言调用 SQL。

    将日期存储为包含时间的日期,然后。使用 Convert 将其转换为日期,您可以像平常一样查询(请参阅转换以了解转换)

  2. 按上述方式存储日期,并在其间使用,因此 $date1=01/01/2011 00:00:00 和 $date2=02/01/2011 00:00:00 会是

    '从日期介于“'.$date1.'”和“'.$date2.'”之间的条目中选择*'
    
  3. 坚持你所拥有的和所做的

    '从日期=“'.$year.'/'.$month.'/'.$day.'”的条目中选择*
    

,但至少在完整的日期和时间下你仍然可以按时间顺序订购它们。

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..

  1. 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)

  2. 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

    'select * from entries where date between "'.$date1.'" and "'.$date2.'"'
    
  3. stick with what you have and do

    'select * from entries where date = "'.$year.'/'.$month.'/'.$day.'"'
    

but at least with the full date and time you could still order them in order of the time.

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