oracle数据库的强大,不言而喻,mysql的精简,实用性也很强。总体而言就单个系统而言oracle强于mysql,但是mysql的集群是相当强大是oracle做不到的,这就是淘宝从分布式oracle转化为mysql集群的最主要的原因之一。
别的撇开不说,首先上两段代码:
oracle版本 :
insert into tmp_common_msg
select '资产单元持仓表中合约:' || stkcode_ || '在证券基本信息表中不存在'
from (select distinct t.stkcode_
from acc_cellstk t, md_stock m
where t.mkcode_ = m.mkcode_(+)
and t.stkcode_ = m.stkcode_(+)
and t.mkcode_ in ('3', '4', '5', '6')
and (m.mkcode_ is null or m.stkcode_ is null))
这段代码在oracle中是完全正确的,但是在mysql中,基本的语法都不过,原因有以下几点:
<1> 懂SQL基本语法的小伙伴都知道,oracle 中多表查询的方式:
包括联合查询、连接查询。连接查询分为内连接、左连接、右连接;oracle中(+)出现在=的右边是左连接 ,相当于left join on ,(+)出现在=的左边是右连接,相当于right join on,缺省(+)是内连接,相当于 inner join on 。
<2> select '资产单元持仓表中合约:' || stkcode_ || '在证券基本信息表中不存在' from这种语法在mysql中是不支持的,对于字符串的拼接在mysql有特定的函数concat(str1,str2,str3,...);
<3>mysql中所有除了真实存在的表,联合查询的表必须都要起个别名。
综合以上三点,mysql版本的代码如下:
insert into tmp_common_msg (msg)
select concat('资产单元持仓表中合约:', tb.stkcode_ ,'在证券基本信息表中不存在')
from (select distinct t.stkcode_
from acc_cellstk t
left join md_stock m
on m.mkcode_ = t.mkcode_
and m.stkcode_ = t.stkcode_
and t.mkcode_ in ('3', '4', '5', '6')
and (m.mkcode_ is null or m.stkcode_ is null)) as tb ;
我也是刚刚开始学习mysql,以上是我在工作中所得收获,特贴出来希望能够对学习oracle转mysql的小伙伴一点帮助,如果我的言论有不真确或者比较偏执的,还请大家不吝指教。