weixin_39711849 2017-08-03 01:37 采纳率: 0%
浏览 2709

编写sql语句数据库当前年份,以及往后推5年的年份,咋写,下面这两种sql不行

select to_char(sysdate,'yyyy') from dual
union
select to_char(sysdate-365,'yyyy') from dual
union
select to_char(sysdate-365*2,'yyyy') from dual
union
select to_char(sysdate-365*3,'yyyy') from dual
union
select to_char(sysdate-365*4,'yyyy') from dual
union
select to_char(sysdate-365*5,'yyyy') from dual
union
select to_char(sysdate+365,'yyyy') from dual
union
select to_char(sysdate+365*2,'yyyy') from dual
union
select to_char(sysdate+365*3,'yyyy') from dual
union
select to_char(sysdate+365*4,'yyyy') from dual
union
select to_char(sysdate+365*5,'yyyy') from dual
网上看的

select extract(year from sysdate) from BO_ACT_MES_SECMAINTENANCE
union
select extract(year from sysdate) -1 from BO_ACT_MES_SECMAINTENANCE
union
select extract(year from sysdate) -2 from BO_ACT_MES_SECMAINTENANCE
union
select extract(year from sysdate) -3 from BO_ACT_MES_SECMAINTENANCE
union
select extract(year from sysdate) -4 from BO_ACT_MES_SECMAINTENANCE
union
select extract(year from sysdate) -5 from BO_ACT_MES_SECMAINTENANCE
union
select extract(year from sysdate) +1 from BO_ACT_MES_SECMAINTENANCE
union
select extract(year from sysdate) +2 from BO_ACT_MES_SECMAINTENANCE
union
select extract(year from sysdate) +3 from BO_ACT_MES_SECMAINTENANCE
union
select extract(year from sysdate) +4 from BO_ACT_MES_SECMAINTENANCE
union
select extract(year from sysdate) +5 from BO_ACT_MES_SECMAINTENANCE

  • 写回答

2条回答 默认 最新

  • 丵鹰 2017-08-03 01:59
    关注

    超过2个sql需要使用union all

    select to_char("ADD_MONTHS"(sysdate, c12),'yyyy') from dual
    union all
    select to_char("ADD_MONTHS"(sysdate, 1*12),'yyyy') from dual
    union all
    select to_char("ADD_MONTHS"(sysdate, 2*12),'yyyy') from dual

    评论

报告相同问题?