如何使用case语句在sql中返回不同的pull?

pw9qyyiw  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(341)

我正在使用presto-sql,并试图使它成为一种可以根据用户输入执行不同select语句的语言。我当前的代码结构如下所示:

SELECT (case when input in ('A', 'B', 'C') then (SELECT * from table1)
             when input in ('D', 'E', 'F') then (SELECT * from table2)
             when input in ('G', 'H', 'I') then (SELECT * from table3)
             ...
        end)

当我这样做时,我得到一个错误,说我不能返回具有多列的子查询。有没有一种方法可以使用这种结构或其他方法来完成单独的select语句?
谢谢

fnatzsnv

fnatzsnv1#

假设表具有相同的列,则使用 union all :

select t.*
from table1 t
where input in ('A', B', 'C')
union all
select t.*
from table2 t
where input in ('D', E', 'F')
union all
. . .

相关问题