2011-05-21 33 views
0

插入一個普通的表,不同的場景如果存在避免多,否則,如果對不同的退出語句中使用聯接內部

如:

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 ........ 

.... 

它就像使用其他條件加盟不同的表不同的ID的... 現在我想編寫一個單一的查詢沒有這其他如果,否則如果...但基於ID值相應的選擇語句應該被稱爲

回答

0

也許你正在尋找定義一個查詢(或查看)使用g SELECT,WHERE和UNION ALL。有關詳細信息,請參閱文檔(postgreSQL,MySQL或任何SQL方言)。