最近写了很多SQL,记录下
查询本年已过去的月份:如 2022-01、2022-02、...、2022-8
Oracle写法
SELECT to_char(sysdate, 'yyyy') || '-' || to_char(add_months(trunc(sysdate, 'yy'), rownum - 1), 'MM') AS TIME
FROM dual CONNECT BY rownum <= TO_NUMBER(TO_CHAR(sysdate, 'MM'))
MySQL写法
SELECT * FROM ( SELECT concat(( SELECT year(curdate()) ), '-01') AS TIME UNION SELECT concat(( SELECT year(curdate()) )
, '-02') UNION SELECT concat(( SELECT year(curdate()) ), '-03') UNION SELECT concat(( SELECT year(curdate()) ), '-04')
UNION SELECT concat(( SELECT year(curdate()) ), '-05') UNION SELECT concat(( SELECT year(curdate()) ), '-06') UNION
SELECT concat(( SELECT year(curdate()) ), '-07') UNION SELECT concat(( SELECT year(curdate()) ), '-08') UNION SELECT
concat(( SELECT year(curdate()) ), '
-09') UNION SELECT concat(( SELECT year(curdate()) ), '-10') UNION SELECT concat(( SELECT year(curdate()) ), '-11')
UNION SELECT concat(( SELECT year(curdate()) ), '-12') ) t2 WHERE STR_TO_DATE(CONCAT(TIME, '-01'), '%Y-%m-%d') <= (
SELECT curdate() FROM dual ) ORDER BY STR_TO_DATE(CONCAT(TIME, '-01'), '%Y-%m-%d')
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏