SELECT * 在 Oracle 中使用 UNION ALL
SELECT * with UNION ALL in Oracle
我有一个要求,我打算从 Oracle DB 中获取重复记录,以模拟我们有重复记录的场景。
所以,我决定使用 union_all
。
select column1 from tbl where rowid in (select rowid from tbl where rownum<=5)
union all
select column1 from tbl where rowid in (select rowid from tbl where rownum<=5)
order by column1;
--works
但是,当尝试获取所有列时,
select * from tbl where rowid in (select rowid from tbl where rownum<=5)
union all
select * from tbl where rowid in (select rowid from tbl where rownum<=5)
order by column1;
-- Doesn't work. Invalid identifier
任何关于此处错误的建议都会有很大帮助。
你可以试试:
select * from (
select * from tbl where rowid in (select rowid from tbl where rownum<=5)
union all
select * from tbl where rowid in (select rowid from tbl where rownum<=5)
)
order by column1;
或显式列出所选列而不是使用 select *
:
For compound queries containing set operators UNION, INTERSECT,
MINUS, or UNION ALL, the ORDER BY clause must specify positions or
aliases rather than explicit expressions. Also, the ORDER BY clause
can appear only in the last component query. The ORDER BY clause
orders all rows returned by the entire compound query.
我有一个要求,我打算从 Oracle DB 中获取重复记录,以模拟我们有重复记录的场景。
所以,我决定使用 union_all
。
select column1 from tbl where rowid in (select rowid from tbl where rownum<=5)
union all
select column1 from tbl where rowid in (select rowid from tbl where rownum<=5)
order by column1;
--works
但是,当尝试获取所有列时,
select * from tbl where rowid in (select rowid from tbl where rownum<=5)
union all
select * from tbl where rowid in (select rowid from tbl where rownum<=5)
order by column1;
-- Doesn't work. Invalid identifier
任何关于此处错误的建议都会有很大帮助。
你可以试试:
select * from (
select * from tbl where rowid in (select rowid from tbl where rownum<=5)
union all
select * from tbl where rowid in (select rowid from tbl where rownum<=5)
)
order by column1;
或显式列出所选列而不是使用 select *
:
For compound queries containing set operators UNION, INTERSECT, MINUS, or UNION ALL, the ORDER BY clause must specify positions or aliases rather than explicit expressions. Also, the ORDER BY clause can appear only in the last component query. The ORDER BY clause orders all rows returned by the entire compound query.