Select same column with multiple conditions using one query

1.9k Views Asked by At

I'm looking for query that can return same results as two given queries:

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)

I need something like:

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

But all I get is error:

Subquery returned more than 1 value

Result that I need:

res1 res2
foo1 foo3
foo2 foo4

How can I get such result using only one query?

1

There are 1 best solutions below

2
Ramin Faracov On

There is no need for subqueries, you can use UNION ALL. Sample:

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)