mysql 能否查询出 本年所有月份

查看:2274
本文介绍了mysql 能否查询出 本年所有月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

能否使用一句 sql 查询出当前年的所有月份,查询出如下形式:

感谢各位大神提供思路。

解决方案

随便找一个大于12条记录的表执行下面的sql就得到你要的结果,不谢^_^

select case when length(mon)=1 then concat('2017-0',mon) else concat('2017-',mon) end months from (select @m:=@m+1 mon from tbl ,(select @m:=0) a ) aa limit 12;


…………………………………………………………………………

要取当年的:

select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months from (select @m:=@m+1 mon from tbl ,(select @m:=0) a ) aa limit 12;

这篇关于mysql 能否查询出 本年所有月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆