分段查询
相比Oracle,PostgreSQL中分段查詢的區別在以下幾點:
1. 沒有rownum項,而是用ROW_NUMBER() OVER()表示;
2. 子查詢必須加別名,例如“as aa”,否則語法檢查不通過;
另外分段查詢的一個注意事項:rownum只能做“<”運算。如果寫“>”、“>=”、“between”的時候,起始值只能是0或1。
因為rownum僅對查詢結果排序生效,當寫出“>1”時,第一條記錄會被排除,然后第二條記錄會變成第一條記錄,接著第一條記錄又被排除,……,所以查詢會成功,但查詢結果必定為空。務必注意這一點!!!
ORACLE
select column_name_a, rownum from table_name_a where column_name_a like 'condition%'; select column_name_a, no from (select column_name_a, rownum no from table_name_a where column_name_a like 'condition%') where no > 0 and no <=5; select column_name_a, no from (select column_name_a, rownum no from table_name_a where column_name_a like 'condition%') where no > 5 and no <=10; select column_name_a, no from (select column_name_a, rownum no from table_name_a where column_name_a like 'condition%') where no > 10 and no <=15;
PGSQL
select column_name_a, no from (select column_name_a, ROW_NUMBER() OVER() as no from table_name_a where column_name_a like 'condition%') as aa where no >0 and no <=5; select column_name_a, no from (select column_name_a, ROW_NUMBER() OVER() as no from table_name_a where column_name_a like 'condition%') as aa where no >5 and no <=10; select column_name_a, no from (select column_name_a, ROW_NUMBER() OVER() as no from table_name_a where column_name_a like 'condition%') as aa where no >10 and no <=15;
PGSQL select column_name_a from table_name_a where column_name_a like 'condition%' limit 5 offset 0; select column_name_a from table_name_a where column_name_a like 'condition%' limit 5 offset 5; select column_name_a from table_name_a where column_name_a like 'condition%' limit 5 offset 10;
1. 沒有rownum項,而是用ROW_NUMBER() OVER()表示;
2. 子查詢必須加別名,例如“as aa”,否則語法檢查不通過;
另外分段查詢的一個注意事項:rownum只能做“<”運算。如果寫“>”、“>=”、“between”的時候,起始值只能是0或1。
因為rownum僅對查詢結果排序生效,當寫出“>1”時,第一條記錄會被排除,然后第二條記錄會變成第一條記錄,接著第一條記錄又被排除,……,所以查詢會成功,但查詢結果必定為空。務必注意這一點!!!
ORACLE
select column_name_a, rownum from table_name_a where column_name_a like 'condition%'; select column_name_a, no from (select column_name_a, rownum no from table_name_a where column_name_a like 'condition%') where no > 0 and no <=5; select column_name_a, no from (select column_name_a, rownum no from table_name_a where column_name_a like 'condition%') where no > 5 and no <=10; select column_name_a, no from (select column_name_a, rownum no from table_name_a where column_name_a like 'condition%') where no > 10 and no <=15;
PGSQL
select column_name_a, no from (select column_name_a, ROW_NUMBER() OVER() as no from table_name_a where column_name_a like 'condition%') as aa where no >0 and no <=5; select column_name_a, no from (select column_name_a, ROW_NUMBER() OVER() as no from table_name_a where column_name_a like 'condition%') as aa where no >5 and no <=10; select column_name_a, no from (select column_name_a, ROW_NUMBER() OVER() as no from table_name_a where column_name_a like 'condition%') as aa where no >10 and no <=15;
帶排序功能的高效寫法
ORACLE
select column_name_a from (select column_name_a, row_number() over (order by sal) no from table_name_a where column_name_a like 'condition%' order by column_name_a) where no > 0 and no <= 5; select column_name_a from (select column_name_a, row_number() over (order by sal) no from table_name_a where column_name_a like 'condition%' order by column_name_a) where no > 5 and no <= 10; select column_name_a from (select column_name_a, row_number() over (order by sal) no from table_name_a where column_name_a like 'condition%' order by column_name_a) where no > 10 and no <= 15;PGSQL select column_name_a from table_name_a where column_name_a like 'condition%' limit 5 offset 0; select column_name_a from table_name_a where column_name_a like 'condition%' limit 5 offset 5; select column_name_a from table_name_a where column_name_a like 'condition%' limit 5 offset 10;
總結
- 上一篇: postgresql中自定义函数脚本的备
- 下一篇: au是什么金