从MySQL中的月份编号中获取月份名称?
您可以使用MONTHNAME()
MySQL中的函数从数字显示月份名称。语法如下。
SELECT MONTHNAME(STR_TO_DATE(yourColumnName,’%m’)) as anyVariableName from yourTableName;
为了理解上述概念,让我们首先创建一个表。创建表的查询如下。
mysql> create table MonthDemo -> ( -> MonthNum int -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into MonthDemo values(1); mysql> insert into MonthDemo values(2); mysql> insert into MonthDemo values(3); mysql> insert into MonthDemo values(4); mysql> insert into MonthDemo values(5); mysql> insert into MonthDemo values(6); mysql> insert into MonthDemo values(7); mysql> insert into MonthDemo values(8); mysql> insert into MonthDemo values(9); mysql> insert into MonthDemo values(10); mysql> insert into MonthDemo values(11); mysql> insert into MonthDemo values(12);
使用select命令显示表中的所有记录。查询如下。
mysql> select *from MonthDemo;
以下是输出。
+------------+ | MonthNum | +------------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | | 10 | | 11 | | 12 | +------------+ 12 rows in set (0.00 sec)
这是显示月份名称的查询。
mysql> select monthname(str_to_date(MonthNum,'%m')) as MonthName from MonthDemo;
以下是输出。
+-----------+ | MonthName | +-----------+ | January | | February | | March | | April | | May | | June | | July | | August | | September | | October | | November | | December | +-----------+ 12 rows in set (0.00 sec)