假设这是我的表 (postgreSQL):
datecolumn a b c
DATE INTEGER INTEGER INTEGER
01.02.17 1 5 7
01.02.16 5 7
02.02.17 5
30.01.17 8
我想编写一个查询,将 CURRENT_DATE
与 datecolumn
进行比较,并显示本月和本年的所有记录
因此对于 CURRENT_DATE
(今天是 2017 年 2 月 2 日),预期结果是:
datecolumn a b c
01.02.17 1 5 7
02.02.17 5
我知道我能做到:
select *
from tab
where extract(month from current_date) = extract(month from datecolumn) and
extract(year from current_date) = extract(year from datecolumn)
但我想知道是否有更简单的方法。
最佳答案
您可以使用date_trunc()
select *
from tab
where date_trunc('month', datecolumn) = date_trunc('month', current_date);
关于sql - 如何查看PostgreSQL中是否已有本月的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41998380/