Postgresql的条件FROM语句



我想根据条件查询不同的表。类似于:

select * FROM
case when :myvar = 'A' then
table A
when :myvar = 'B' then
table B
else
table C
end

编辑:所有表都有相同的列,但:myvar是外部设置的变量。:myvar与任何列中的值都不对应

如果所有表都有相同的列,则使用union all:

select a.* 
from a
where :myvar = 'A'
union all
select b.*
from b
where :myvar = 'B'
union all
select c.*
from c
where :myvar not in ('A', 'B');

如果它们没有相同的列,则从所有表中选择select公共列。或者,放弃:单个select查询返回一组固定的列。

最新更新