postgresql 选择日期字段在给定月份和年份的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5052656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 22:49:56  来源:igfitidea点击:

Select lines whose date-field is in a given month and year

postgresqldate

提问by oriander

My SQL table looks like this:

我的 SQL 表如下所示:

id (int) | date (date) | text1 (varchar) | text2 (varchar)

I want to select the lines whose date suits a given month and year, regardless of the day. Both month and year are given in the select-statement as integers. So the missing thing is the where-clause. Perhaps extract()is the thing I'm looking for, but I don't know how to use it with the two integers, e.g. 2011and 02.

我想选择日期适合给定月份和年份的行,而不考虑日期。月份和年份都在选择语句中作为整数给出。所以缺少的是 where 子句。也许extract()是我正在寻找的东西,但我不知道如何将它与两个整数一起使用,例如201102

回答by Mark Byers

You can use extract:

您可以使用提取物:

SELECT * FROM yourtable
WHERE EXTRACT(month FROM "date") = 2
AND EXTRACT(year FROM "date") = 2011

But in this case you could also do this:

但在这种情况下,您也可以这样做:

SELECT * FROM yourtable
WHERE "date" >= '2011-02-01' AND "date" < '2011-03-01'