如何组合多个选择以从多个表中获取数据

时间:2018-08-20 11:01:12

标签: sql oracle

我遇到这样的情况,我应该从不同的表中选择带有查询的数据,如下所示,它是使用每一步逐行完成的。像tab1和tab2这样的表具有关系,但是有人可以举一个例子,说明如何在一个SQL SELECT子句中查询这些值吗?谢谢!

-- First query to get field value to next select<br>
SELECT tab1.*, tab2.*  FROM tab1, tab2 WHERE field1 = 'Key1'; <br>
SELECT * from tab2 WHERE field2 = 'Key2'; -- returned from row 1 e.g. tab1.field<br>
select * FROM tab3 where field3 = 'Key3'; -- returned from row 2 e.g. tab2.field<br>
SELECT * FROM tab4 WHERE field4 in ('Key4','Key5','Key7'); <br>
-- returned from row 3 e.g tab3 with three fields<br>

1 个答案:

答案 0 :(得分:0)

如果您希望以一个tableview的形式获得结果,请尝试这样使用, 或者您应该在相关列上使用innerjoin

 SELECT *
  FROM tab1 
  WHERE tab1=key1
 UNION  
 SELECT * 
  FROM tab2 
  WHERE tab2=key2
 UNION  
 SELECT * 
  FROM tab3 
  WHERE tab3=key3
 UNION  
 SELECT * 
  FROM tab4 
     WHERE tab4 in ('Key4','Key5','Key7');