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
