如何在 MySQL 中从时间戳获取星期名称?
mysqlmysqli database
要从时间戳获取星期名称,请使用 dayname() 函数 −
select dayname(yourColumnName) from yourTableName; Let us first create a table : mysql> create table DemoTable ( LoginDate timestamp ); Query OK, 0 rows affected (0.52 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('2019-06-01'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('2019-06-02'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('2019-06-03'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('2019-06-04'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2018-06-21'); Query OK, 1 row affected (0.09 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
输出
+-----------------------+ | LoginDate | +-----------------------+ | 2019-06-01 00 :00 :00 | | 2019-06-02 00 :00 :00 | | 2019-06-03 00 :00 :00 | | 2019-06-04 00 :00 :00 | | 2018-06-21 00 :00 :00 | +-----------------------+ 5 rows in set (0.00 sec)
以下是在 MySQL 中从时间戳获取星期名称的查询 −
mysql> select dayname(LoginDate) from DemoTable;
输出
+--------------------+ | dayname(LoginDate) | +--------------------+ | Saturday | | Sunday | | Monday | | Tuesday | | Thursday | +--------------------+ 5 rows in set (0.00 sec)