【问题标题】:Avoiding multiple if exists, else if exits statement for different joins used inside避免多个 if exists, else if exits 用于内部使用的不同连接的语句
【发布时间】: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语句

【问题讨论】:

    标签: sql join if-statement exists


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      • 1970-01-01
      相关资源
      最近更新 更多