将我的日期月份名称(编号)转换为SQL查询中的单词

时间:2017-04-12 03:36:18

标签: mysql date

请帮我转换我的查询,该查询返回以月为单位的数字格式。我想把它转换为像1月这样的词。

这是我的问题:

SELECT
    MONTH(tbl_check_up.check_up_date) AS datemonths
FROM
    tbl_clinics
INNER JOIN 
    tbl_queue ON tbl_clinics.clinic_id = tbl_queue.clinic_id
INNER JOIN 
    tbl_check_up ON tbl_queue.queue_id = tbl_check_up.queue_id
WHERE 
    tbl_clinics.user_id = 102 
    AND YEAR(tbl_check_up.check_up_date) = 2017
GROUP BY 
    MONTH(tbl_check_up.check_up_date)

我的查询输出是:

--------------
| datemonths |
--------------
|     2      |
|     4      |
|____________|

2 个答案:

答案 0 :(得分:1)

尝试

 DATE_FORMAT(date_column,'%M')

你的sql将如下所示

SELECT
DATE_FORMAT(tbl_check_up.check_up_date,'%M') AS datemonths
FROM
tbl_clinics
INNER JOIN tbl_queue ON tbl_clinics.clinic_id = tbl_queue.clinic_id
INNER JOIN tbl_check_up ON tbl_queue.queue_id = tbl_check_up.queue_id
WHERE tbl_clinics.user_id = 102 AND YEAR(tbl_check_up.check_up_date) = 2017
GROUP BY DATE_FORMAT(tbl_check_up.check_up_date,'%M')

答案 1 :(得分:0)

是的,请使用STR_TO_DATE()。阅读它here