將代碼從 Oracle 遷移到 Postgres:
甲骨文代碼:
SELECT *
FROM (SELECT customers.*
FROM customers
WHERE customer_id > 4500
ORDER BY last_name)
WHERE ROWNUM < 3;
據我所知,如果必須使用 rownum 限制輸出,我們需要添加子查詢以獲得正確的排序結果是 Oracle 的限制。
Postgres 是否也有同樣的限制,或者我可以將 Posgtes 中的代碼轉換為單個查詢嗎?
SELECT customers.*
FROM customers
WHERE customer_id > 4500
ORDER BY last_name
LIMIT 3;
uj5u.com熱心網友回復:
您的假設是正確且易于測驗的:
drop table if exists test_70069841;
create table test_70069841 as select 2;
insert into test_70069841 select 3;
insert into test_70069841 select 5;
insert into test_70069841 select 6;
insert into test_70069841 select 7;
insert into test_70069841 select 1;
select * from test_70069841;
-- ?column?
------------
-- 2
-- 3
-- 5
-- 6
-- 7
-- 1
--(6 rows)
select * from test_70069841 order by 1;
-- ?column?
------------
-- 1
-- 2
-- 3
-- 5
-- 6
-- 7
--(6 rows)
select * from test_70069841 order by 1 limit 3;
-- ?column?
------------
-- 1
-- 2
-- 3
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/363539.html
標籤:sql PostgreSQL的
上一篇:一次處理多個存盤程序狀態
