避免多个 if exists, else if exits 用于内部使用的不同连接的语句

Posted

技术标签:

【中文标题】避免多个 if exists, else if exits 用于内部使用的不同连接的语句【英文标题】:Avoiding multiple if exists, else if exits statement for different joins used inside 【发布时间】:2011-05-21 11:07:51 【问题描述】:

将不同场景插入到一个通用表中

例如:

if exists(select id from tablename where  id =1 )
insert into Temp 
select * from tbl inner join ........
else if exists(select id from tablename where id=2)
insert into Temp 
select * from tb2 inner join ........
else if exists(select id from tablename where id=3 )
insert into Temp 
select * from tb3 inner join ........

....

这就像使用 else 条件来为不同的 id 连接不同的表...... 现在我想写一个没有这个else if,else if ...的单个查询,但是根据id值应该调用相应的select语句

【问题讨论】:

【参考方案1】:

也许您希望使用 SELECT、WHERE 和 UNION ALL 定义查询(或视图)。有关详细信息,请参阅文档(postgreSQL、mysql 或任何 SQL 方言)。

【讨论】:

以上是关于避免多个 if exists, else if exits 用于内部使用的不同连接的语句的主要内容,如果未能解决你的问题,请参考以下文章