psql的时间类型,通过时间查询
psql的时间类型,通过时间查询
psql有date/timestamp类型,date只显示年月日1999-01-08,而timestamp显示年月日时分秒 1999-01-08 09:54:03.249
在我们写sql语句,根据时间进行查询的时候
有下面一张表demo,
Column | Type | Collation | Nullable | Default |
id | integer | not null | nextval('build_log_id_seq'::regclass) | |
content | character varying(50) | not null | NULL::character varying | |
createDate | timestamp without time zone | not null | NULL::character varying | |
write_date | date | not null | NULL::character varying |
表内数据:
id | content | createDate | write_date |
1 | 内容1 | 2018-04-26 09:54:03.221 | 2018-01-02 |
2 | 内容2 | 2018-02-13 08:15:12.222 | 2018-01-03 |
3 | 内容3 | 2018-06-18 19:15:12.222 | 2018-01-04 |
4 | 内容4 | 2018-08-12 12:15:12.22 | 2018-01-05 |
表中的write_date是date类型可以直接使用=,select * from table where time='2018-01-02'
表中的createDate是timestamp类型,如果使用=号,但是我们不能精确到十分秒,所以用=号就不会查出内容,
我们可以使用><或者使用between and语句
例如:
查询4月26日创建的数据:
select * from demo where createDate >= '2018-04-26' and createDate < '2018-04-27'
或者:
select * from demo where createDate between '2018-04-26' and '2018-04-27'
还有一种,我们将字符串转成时间类型,
select * from demo where createDate >= '2018-04-26'::date and createDate < ( '2018-04-26'::date + '1 day'::interval )
表内数据
[Biǎo nèi shùjù]
In the data table
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现