pg和oracle比较,PostgreSQL与Oracle的sql差异

### 1.rownum

#### (1)Oracle分页查询使用rownum,PostgreSQL使用limit offset

Oracle | PostgreSQL

-|-|-

select \* from (select rownum r,e.\* from emp e where rownum <=5) t where r>0;| select \* from emp limit 5 offset 0;

####(2)Oracle中rownum=1,PostgreSQL中使用limit 1

Oracle | PostgreSQL

-|-|-

select \* from emp where rownum = 1;| select \* from emp limit 1;

#### (3)Oracle中序号列rownum,PostgreSQL使用窗口函数

Oracle | PostgreSQL

-|-|-

select rownum,t.\* from emp t;| select row_number() over(), t.* from emp t;

### 2.系统日期

Oracle | PostgreSQL

-|-|-

SYSDATE| current_timestamp, current_date

### 3.delete语句

Oracle delete语句可以没有from,pg必须要有from

Oracle | PostgreSQL

-|-|-

delete from emp where empno = xxx;

delete emp where empno = xxx| delete from emp where empno = xxx

### 4.类型自动转换

Oracle支持类型自动转换,例如数字自动换换为字符串等;PG中需要显示转换,或者添加CAST

### 5.子查询别名

PostgreSQL在from关键字后的子查询必须要有别名,Oralce可以没有。

### 6. group by having

PG having语句必须在group by之后,oracle可以在group by之前

### 7.递归查询

Oracle中使用start with ... connect by..., PG中使用with recusive

Oracle | PostgreSQL

-|-|-

select \*

from larearrelation

where rearedgens = 1

and rearflag = 1

and rearlevel = '01'

connect by prior agentcode = rearagentcode

start with rearagentcode = '10032226';

| with recursive rs as (

select \* from larearrelation where rearagentcode = '10032226'

union all

select a.* from larearrelation a, rs where a.rearagentcode = rs.agentcode

)

select \** from rs where rearedgens = 1 and rearflag = '1' and rearlevel = '01'

###8.update语句别名

postgresql中update语句时,set的字段不能有别名

Oracle | PostgreSQL

-|-|-

update emp t set t.name = 'xxx' where t.empno = 2| update emp set name = 'xxx' where empno = 2

### 9. 日期相减

oracle日期相减自动转换为数字,结果为相差的天数。

pg日期相减为interval类型,得到相差天数需要进行类型转换

### 10.递归查询中的level

oracle的递归查询中level表示查询深度(或者递归层次),在PG中没有此含义的关键字,需要自行在with recursive实现

Oracle | PostgreSQL

-|-|-

select max(level) from larearrelation

where rearedgens = 1

and rearflag = 1

and rearlevel = '01'

connect by prior agentcode = rearagentcode

start with rearagentcode = '10032226';

| with recursive rs as (

select larearrelation.*, 1 depth from larearrelation where rearagentcode = '10032226'

union all

select a./**, rs.depth + 1 depth from larearrelation a, rs where a.rearagentcode = rs.agentcode

)

select max(rs.depth) from rs where rearedgens = 1 and rearflag = '1' and rearlevel = '01'

### 11.序列的调用

Oracle | PostgreSQL

-|-|-

select seqname.nextval from dual;| select nextval('seqname')

### 12.外连接

Oralce外连接支持使用 (+), PostgreSQL需使用left jion或者right join标准sql语法

### 13.distinct去重复

oracle支持unique关键字去重复,pg中只能使用distinct

### 14.字符串分割

Oracle | PostgreSQL

-|-|-

listagg| string_agg

### 15.集合相减

Oracle | PostgreSQL

-|-|-

Minus | except

### 16.null与"

null和’’在oracle中是一致的,最终都会存储为null,在PG中会进行区分

### 17.不等于

Oracle中 ! =,< >操作符中间允许有空格,PG中不可以

### 18.别名

PG中无效的别名,可以尝试加as关键字,例如name

### 19.正则表达式

Oracle | PostgreSQL

-|-|-

SELECT REGEXP_SUBSTR('17,20,23','[^,]+',1,1,'i') FROM DUAL;| select (regexp_matches('17,20,23', '[^,]+'))[1]

### 20.字段大小写

oracle字段名大写,PG字段名小写

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/69956102/viewspace-2666880/,如需转载,请注明出处,否则将追究法律责任。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值