1. 程式人生 > >MySQL與Oracle 差異比較-基本語法

MySQL與Oracle 差異比較-基本語法

1、 查詢行數自增1

Oracle:中select rownum , a.* from fdcom a

Mysql:

select @ROWNO := @ROWNO + 1 as rownum , a.*

from (select shortname

      from fdcom

     where comtype='02' and comkind='02'

) a,(SELECT @ROWNO := 0) a1

2、 日期格式轉換

Oracle: to_date(makedate,’yyyy-mm-dd’)

Mysql: date_format(e.makedate,’%Y-%m-%d’)

#轉換為月日

Oracle:select to_char(date’2016-11-1’,‘mmdd’) from dual

Mysql:select date_format(date(‘2016-11-1’),’%m%d’) from dual

select date_format(‘2016-11-1’,’%m%d’) from dual

#轉換為年

Oracle:select to_char(date’2016-11-1’,‘YYYY’) from dual

Mysql:select date_format(‘2016-11-1’,’%Y’) from dual

3、 拼接字元

Oracle: ||

Mysql: 替換為concat(‘’,‘’)

4、 Oracle :nvl

Mysql:替換為ifnull()

5、 兩個日期時間計算

Oracle: select months_between(sysdate, departdate) from fadimission

mysql: select timestampdiff(month,‘2016-12-1’,‘2016-9-1’) from dual

6、數字格式轉換

Oracle:

select CAST(round(0.01,5) AS CHAR(6)) from dual

mysql:

select cast(0.123 as decimal(5,4)) from dual

7、行數限制

Oracle:rownum=1

Mysql:limit 1 (可以直接在where後,但是不能在直接and後)

8、有巢狀查詢,mysql最外層一定要加別名

select comcode

from (select comcode from fdcom where comkind = ‘02’) a

where 1 = 1

9、mysql:delete 後需要加上from

10、settledate取日期範圍問題

Oracle:

select add_months(date’2017-4-11’,-1-0+1) from dual

select last_day(add_months(date’2017-4-11’,-0)) from dual

myqsql:

SELECT DATE_ADD(‘2017-04-11’,INTERVAL 0 month) from dual

select last_day(date_add(‘2017-04-11’,interval -0 month)) from dual

11、

別名問題:

增加紅色框中的內容可以解決別名的問題

12、connect by 的問題

(1) connect by prior upcomcode=comcode 這種從下向上查的可以用程式替換,也可以用函式替換

程式參考: GetFHManageComLimit類中的方法parseUpManageComLimitlikeNew,查的是所有機構

Js方法參考:Agentcommon2.js的getBasicLawToComCode中的getSortListNew這個方法查的是comtype=03

從下向上查的還可以用方法:

select ComCode from FDCom where ComType = ‘03’ connect by PRIOR UpComCode = ComCode start with ComCode =’’

限制了層級的可以用select queryUpcomcode (comcode,comtype) from dual

沒有限制層級的用queryComcodeNew(comcode)查的是包含comcode及各層上級編碼

(2) connect by prior comcode = upcomcode 從上往下查

可以直接 incomcode like concat((select incomcode from fdcom where commcode =’’),’%’)

同樣的保險公司從下向上查滿足某個層級的用select queryINSSuperCode(suppliercode, insclass) from dual

13、oracle :decode

Mysqsl:轉換為case 。。when 。。end

14:oracle:sysdate

Mysql:sysdate()

15:查代理人上級主管

connect by PRIOR UpAgent=AgentCode

用queryUpagent方法