Sql 获取当月内的行
从psql中按当前月份筛选的表中获取行 示例表-Sql 获取当月内的行,sql,postgresql,Sql,Postgresql,从psql中按当前月份筛选的表中获取行 示例表- +--------------+--------------------+--------------------------+ | id | name | registeration_date | +--------------------------------------------------------------+ | 1 | abc
+--------------+--------------------+--------------------------+
| id | name | registeration_date |
+--------------------------------------------------------------+
| 1 | abc | 2018|12|31 18:30:00|00 |
+--------------------------------------------------------------+
| 2 | pqr | 2018|11|31 18:30:00|00 |
+--------------------------------------------------------------+
| 3 | lmn | 2020|07|10 18:30:00|00 |
+--------------+--------------------+--------------------------+
查询结果后,假设当月为2020年7月:
+--------------+--------------------+--------------------------+
| id | name | registeration_date |
+--------------+--------------------+--------------------------+
| 3 | lmn | 2020|07|10 18:30:00|00 |
+--------------+--------------------+--------------------------+
在这里,我使用
Extract
函数。有关提取的更多详细信息,请查看
在这里,我使用Extract
函数。有关提取的更多详细信息,请查看
通常的方法是使用范围查询,以允许在注册日期使用索引
select *
from the_table
where registration_date >= date_trunc('month', current_date)
and registration_date < date_trunc('month', current_date) + interval '1 month';
选择*
从桌子上
其中注册日期>=日期(“月”,当前日期)
注册日期<日期(月),当前日期)+间隔时间“1个月”;
通常的方法是使用范围查询,以允许在注册日期使用索引
select *
from the_table
where registration_date >= date_trunc('month', current_date)
and registration_date < date_trunc('month', current_date) + interval '1 month';
选择*
从桌子上
其中注册日期>=日期(“月”,当前日期)
注册日期<日期(月),当前日期)+间隔时间“1个月”;
如果我们使用提取物,会有什么不同?我无法跟踪索引使用情况。如果在registration\u date
上有索引,此查询可以利用它来提高性能。使用extract()
的替代方法不能,并且必须扫描整个表,这通常比较慢。如果使用extract,会有什么区别?我无法跟踪索引使用情况。如果在registration\u date
上有索引,此查询可以利用它来提高性能。使用extract()
的替代方法不能,并且必须扫描整个表,这通常比较慢。