PostgreSQL和Oracle的sql差异
PostgreSQL和Oracle的sql差异
1.rownum
(1)Oracle分页查询使用rownum,PostgreSQL使用limit offset
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.序列的调用
序列: https://www.cnblogs.com/mchina/archive/2013/04/10/3012493.html
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字段名小写
21.NVL --- COALESCE
ORACLE | POSTGRESQL |
---|---|
SELECT NVL(SUM(VALUE1),0) S_VALUE1 FROM FIELD_SUM ; | SELECT COALESCE(SUM(VALUE1),0) AS S_VALUE1 FROM FIELD_SUM ; |
22.TO_NUMBER
ORACLE | POSTGRESQL |
---|---|
SELECT COL1 FROM A_TABLE ORDER BY TO_NUMBER(COL1); | SELECT COL1 FROM A_TABLE ORDER BY TO_NUMBER(COL1, 999999) [注:'999999' ---- 6位数为COL1字段的长度] |
23.DECODE --- CASE WHEN
ORACLE | POSTGRESQL |
---|---|
SELECT DECODE(ENDFLAG, '1', 'A', 'B') ENDFLAG FROM TEST; | SELECT (CASE ENDFLAG WHEN '1' THEN 'A' ELSE 'B' END) AS ENDFLAG FROM TEST; |
24.ADD_MONTHS
ORACLE | POSTGRESQL |
---|---|
add_months(date, int) |
--创建函数来解决 RETURNS date AS 'SELECT ($1 + ( $2::text || ''months'')::interval)::date;' LANGUAGE 'sql' |
25.LAST_DAY
ORACLE | POSTGRESQL |
---|---|
LAST_DAY(DATE) |
--创建函数来解决 RETURNS DATE AS 'SELECT date(substr(text($1 + interval(''1 month'')),1,7)||''-01'')-1' LANGUAGE 'sql'; |
26.LAST_DAY
ORACLE | POSTGRESQL |
---|---|
LAST_DAY(DATE) |
--创建函数来解决 RETURNS DATE AS 'SELECT date(substr(text($1 + interval(''1 month'')),1,7)||''-01'')-1' LANGUAGE 'sql'; |
未完待续。。。。。。
作者: BORS