ORACLE中几分钟写出来的SQL,需要用MYSQL实现,结果卡住了,主要是不知道MYSQL怎么解决时间的拆分,两段时间如果跨年,中间以一年的12月31号拆分成多行。 SELECT L.EMP_NAME, TO_CHAR(L.LEAVE_END_DATE, 'yyyy') - ROWNUM + 1 YEAR, CASE WHEN TO_CHAR(L.LEAVE_END_DATE, 'yyyy') - ROWNUM + 1 = TO_CHAR(L.LEAVE_START_DATE, 'yyyy') THEN TO_CHAR(LEAVE_START_DATE, 'yyyy-mm-dd hh24:mi') ELSE TO_CHAR(L.LEAVE_END_DATE, 'yyyy') - ROWNUM + 1 || '-01-01 00:00' END LEAVE_START_DATE, CASE WHEN TO_CHAR(L.LEAVE_END_DATE, 'yyyy') - ROWNUM + 1 = TO_CHAR(L.LEAVE_END_DATE, 'yyyy') THEN TO_CHAR(LEAVE_END_DATE, 'yyyy-mm-dd hh24:mi') ELSE TO_CHAR(L.LEAVE_END_DATE, 'yyyy') - ROWNUM + 1 || '-12-31 23:59' END LEAVE_END_DATE, L.PROCESSCODE || '_' || ROWNUM PROCESSCODE FROM DUAL LEFT JOIN (SELECT '测试' EMP_NAME, '2016' YEAR, TO_DATE('2017-02-13 8:30', 'yyyy-mm-dd hh24:mi') LEAVE_START_DATE, TO_DATE('2019-05-22 8:30', 'yyyy-mm-dd hh24:mi') LEAVE_END_DATE, 'AAA' PROCESSCODE FROM DUAL) L ON 1 = 1 CONNECT BY ROWNUM <= TO_CHAR(L.LEAVE_END_DATE, 'yyyy') - TO_CHAR(L.LEAVE_START_DATE, 'yyyy') + 1 |