以下是示例:
ID | value
1 51
2 25
3 11
4 27
5 21
我需要获得前三个参数并将它们放入变量中,例如out_x,out_y,out_z。 没有多次选择可以做到吗?
答案 0 :(得分:0)
您可以这样做:
select max(case when id = 1 then value end),
max(case when id = 2 then value end),
max(case when id = 3 then value end)
into out_x, out_y, out_z
from t
where id in (1, 2, 3);
但是,我想三种形式的查询:
select value into out_x
from t
where id = 1;
是一种更清洁的方法。
答案 1 :(得分:0)
您可以使用PIVOT
:
SELECT x, y, z
INTO out_x, out_y, out_z
FROM your_table
PIVOT ( MAX( value ) FOR id IN ( 1 AS x, 2 AS y, 3 AS z ) )
或者,如果你不知道你需要哪个ID
(但只想要前3个),那么:
SELECT x, y, z
INTO out_x, out_y, out_z
FROM (
SELECT value, ROWNUM AS rn
FROM ( SELECT value FROM your_table ORDER BY id )
WHERE ROWNUM <= 3
)
PIVOT ( MAX( value ) FOR rn IN ( 1 AS x, 2 AS y, 3 AS z ) )