只输出 MySQL 中月份的名称,而不是月份的数字
要仅显示月份数字,请使用 DATE_FORMAT( ) 以及 STR_TO_DATE( )。我们首先创建一个表格
mysql> create table DemoTable1320 -> ( -> MonthName varchar(20) -> ); Query OK, 0 rows affected (0.43 sec)
示例
使用插入命令在表中插入一些记录 −
mysql> insert into DemoTable1320 values('10/10/2010'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1320 values('11/12/2018'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1320 values('12/01/2019'); Query OK, 1 row affected (0.15 sec)
使用选择语句从表中显示所有记录 −
mysql> select *from DemoTable1320;
输出
+------------+ | MonthName | +------------+ | 10/10/2010 | | 11/12/2018 | | 12/01/2019 | +------------+ 3 rows in set (0.00 sec)
示例
以下是对月份名称而非数字进行输出的查询 −
mysql> select date_format(str_to_date(MonthName,'%d/%m/%Y'),'%M') from DemoTable1320;
输出
+-----------------------------------------------------+ | date_format(str_to_date(MonthName,'%d/%m/%Y'),'%M') | +-----------------------------------------------------+ | October | | December | | January | +-----------------------------------------------------+ 3 rows in set (0.00 sec)
广告