Home  >  Article  >  Database  >  Oracle 迁移至MySQL部分语句的转换

Oracle 迁移至MySQL部分语句的转换

WBOY
WBOYOriginal
2016-06-07 17:35:421189browse

前几天把系统从oracle往mysql上迁移,很多的语句是比较简单的,就是一些函数的修改如to-date等

Oracle 迁移至MySQL部分语句的转换

[日期:2014-02-27] 来源:Linux社区  作者:jimmy609 [字体:]

前几天把系统从Oracle往mysql上迁移,很多的语句是比较简单的,就是一些函数的修改如to-date等

但是也有几个比较棘手的,这里记录下

第一、row_number() over(partition by 

首先要了解下oracle中这个函数的用法,看个例子

select t.*,row_number() over(partition by t.owner order by y.createDate desc) rn from test t

这个语句的意思就是,把test这个表的数据按照owner 分组并且给每个分组的里面的数据加上一个序列号,数据格式如下

id      name      owner  createDate  rn

1        aa            001                              1

2          bb          001                              2

3          cc            001                            3

4          dd            002                            1

5          ee          002                              2

6            ff            003                              1

数据搞得不太正规,但是应该能够看懂它的意思吧,

但是在mysql中是没有这个函数的于是乎,找啊找,,总算找了个解决方法,如下

SELECT
    heyf_tmp.*,
    IF(@pdept=heyf_tmp.owner ,@rn:=@rn+1,@rn:=1) AS rn,
    @pdept:=heyf_tmp.owner
FROM
    (
        SELECT
            yv.*
        FROM
            test yv
        ORDER BY
            yv.owner  ,
            yv.createDate  DESC
    )
    heyf_tmp ,
    (
        SELECT
            @rn :=0 ,
            @pdept := NULL ,
            @rn:=0
    )
    aa


具体是什么意思,不是很清楚,不过先解决问题再说

第二、oracle树形查询

oracle树形查询现成的方法

select distinct t.id as id, t.name

from test t
                start with id=‘’
              connect by prior id = parentid

但是mysql中是没有这个方法的,于是只能自己定义函数或者过程,我这里用的是过程

如下

CREATE PROCEDURE Pro_GetTreeList`(in pid varchar(36))
begin
  declare lev int;
  set lev=1;
  drop table if exists tmp1;   
  CREATE TABLE tmp1(id VARCHAR(40),name varchar(50),parentid varchar(40) ,levv INT);   
  INSERT tmp1 SELECT id,name,parent_id,1 FROM `test` WHERE parent_id=pid;   
  while  row_count()>0
    do    set lev=lev+1;   
    INSERT tmp1 SELECT t.id,t.name,t.parent_id,lev from testt join tmp1 a on t.parent_id=a.id AND levv=lev-1;--查出子节点
  end while ;   
  INSERT tmp1 SELECT id,name,parent_id,0 FROM test WHERE id=pid;  --查出当前节点
end


这个存储过程应该都能看懂吧,就不做多解释了

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn