我可以从表中选择一个行列表。但我想通过颠倒交换来展示它们。 释:
with table1 as
(
select 1 ID, 'txt1' value from dual
union all
select 2, 'txt2' from dual
union all
select 7, 'txt7' from dual
union all
select 5, 'txt5' from dual
union all
select 3, 'txt3' from dual
)
select * from table1;
在上面的查询中我可以获得以下结果
ID | VALUE
------------------
1 txt1
2 txt2
7 txt7
5 txt5
3 txt3
但我想将它们显示如下
ID | VALUE
------------------
3 txt3
5 txt5
7 txt7
2 txt2
1 txt1
怎么做?
答案 0 :(得分:2)
一种方法是将计算列添加到您的联合查询集中,然后按该列排序:
WITH table1 AS (
SELECT 1 ID, 'txt1' value, 1 AS position FROM dual
UNION ALL
SELECT 2, 'txt2', 2 FROM dual
UNION ALL
SELECT 7, 'txt7', 3 FROM dual
UNION ALL
SELECT 5, 'txt5', 4 FROM dual
UNION ALL
SELECT 3, 'txt3', 5 FROM dual
)
SELECT *
FROM table1
ORDER BY pos DESC;
请注意,一般情况下,SQL表没有内部命令。实际上,即使是您正在观察的当前订单也不一定由Oracle保证。如果您希望结果集中有某个订单,则需要通过ORDER BY
子句强加它。
答案 1 :(得分:1)
这是怎么回事?
with table1 as
(
select 1 ID, 'txt1' value from dual
union all
select 2, 'txt2' from dual
union all
select 7, 'txt7' from dual
union all
select 5, 'txt5' from dual
union all
select 3, 'txt3' from dual
)
select * from table1 order by rownum desc;
实际上这不适用于这个特定的例子。但它适用于普通表。