0

I have situation where I should select data from different tables with on query like below it's done using each step row by row. Tables like tab1 and tab2 has relationsbut can someone give a example how to query these values in one single SQL SELECT clause? Thanks!

-- 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>
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
ccccccc
  • 99
  • 3
  • 9

1 Answers1

0

If you want to get result as one tableview try to use like this, or you should use innerjoin on related columns

 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');
gugy
  • 21
  • 2