2

我正在寻找可以返回与两个给定查询相同结果的查询:

select foo as res1 from table t where t.id in (1,2)
select foo as res2 from table t where t.id in (3,4)

我需要类似的东西:

select
(select foo from table t where t.id in (1,2)) as res1,
(select foo from table t where t.id in (3,4)) as res2

但我得到的只是错误:

子查询返回超过 1 个值

我需要的结果:

资源1 资源2
foo1 foo3
foo2 foo4

如何仅使用一个查询获得这样的结果?

4

1 回答 1

0

不需要子查询,你可以使用UNION ALL. 样本:

select foo as res1 from table t where t.id in (1,2)
union all 
select foo as res2 from table t where t.id in (3,4)
于 2021-12-10T21:49:41.357 回答