在MySQL中仅输出月份名称,而不输出月份号
要仅显示月份号,请使用DATE_FORMAT()和STR_TO_DATE()。让我们首先创建一个表:
mysql> create table DemoTable1320 -> ( -> MonthName varchar(20) -> );
示例
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1320 values('10/10/2010'); mysql> insert into DemoTable1320 values('11/12/2018'); mysql> insert into DemoTable1320 values('12/01/2019');
使用select语句显示表中的所有记录-
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)