從 MySQL 中的時間戳值中僅顯示日期


要僅從時間戳值中顯示日期,請在 MySQL 中使用 FROM_UNIXTIME() 方法。我們首先建立一個表 −

mysql> create table DemoTable
   -> (
   -> timestampValue bigint
   -> );
Query OK, 0 rows affected (0.70 sec)

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

mysql> insert into DemoTable values(1538332200);
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values(1577730600);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(1488652200);
Query OK, 1 row affected (0.12 sec)

使用 select 語句從表中顯示所有記錄 −

mysql> select *from DemoTable;

這將產生以下輸出 −

+----------------+
| timestampValue |
+----------------+
|     1538332200 |
|     1577730600 |
|     1488652200 |
+----------------+
3 rows in set (0.00 sec)

以下是查詢以從時間戳值中顯示日期 −

mysql> select from_unixtime(timestampValue) as Date from DemoTable;

這將產生以下輸出 −

+---------------------+
| Date                |
+---------------------+
| 2018-10-01 00:00:00 |
| 2019-12-31 00:00:00 |
| 2017-03-05 00:00:00 |
+---------------------+
3 rows in set (0.00 sec)

更新於:17-12-2019

185 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.