查詢 MySQL 表並獲取過去 3 天之前釋出的行?


假設當前日期為 -

'2019-10-20

我們首先會看到一個示例並建立一個表 -

mysql> create table DemoTable1582
   -> (
   -> PostedDate datetime
   -> );
Query OK, 0 rows affected (13.36 sec)

使用 insert 命令在表中插入一些記錄 -

mysql> insert into DemoTable1582 values('2019-01-21 12:34:40');
Query OK, 1 row affected (1.06 sec)
mysql> insert into DemoTable1582 values('2019-10-15 11:00:00');
Query OK, 1 row affected (0.87 sec)
mysql> insert into DemoTable1582 values('2019-10-25 1:10:00');
Query OK, 1 row affected (1.14 sec)

使用 select 語句顯示錶中的所有記錄 -

mysql> select * from DemoTable1582;

這會生成以下輸出 -

+---------------------+
| PostedDate          |
+---------------------+
| 2019-01-21 12:34:40 |
| 2019-10-15 11:00:00 |
| 2019-10-25 01:10:00 |
+---------------------+
3 rows in set (0.00 sec)

以下是檢索過去 3 天釋出的行內容的查詢。

mysql> select * from DemoTable1582
   -> where PostedDate between date_sub(PostedDate, interval 3 day) and now();

這會生成以下輸出 -

+---------------------+
| PostedDate          |
+---------------------+
| 2019-01-21 12:34:40 |
| 2019-10-15 11:00:00 |
+---------------------+
2 rows in set (0.20 sec)

更新於: 2019-12-16

124 次瀏覽

開始 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.