Click here to Skip to main content
15,894,138 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
select *
from t1
where exists(select * from t2 where t2.col1 = t1.col1 and t2.col2 = t1.col2)



how to modify same query for four tables

Please give me a Help
Posted
Updated 25-May-12 23:22pm
v2
Comments
db7uk 26-May-12 9:32am    
Could you not use a union statement?
Sandeep Mewara 27-May-12 11:18am    
You talking of joining table and having all in a result set or getting a list of columns via query? What does unique mean?

If your question is about the tables having same column names and you want to rename them in the result set, then you should define aliases for them. Something like:
SQL
SELECT t1.Col1 AS T1_col1,
       t1.Col2 AS T1_col2,
       t2.Col1 AS T2_col1,
       t2.Col2 AS T2_col2,
       ...
FROM  Table1 t1 INNER JOIN Table2 t2 ON t1.PrimaryKeyCol =  t2.ForeignKeyCol
      ...
WHERE ...
 
Share this answer
 
Comments
Maciej Los 29-May-12 4:55am    
+5!
Wendelius 1-Jun-12 0:46am    
Thanks :)
if you want unique name than you could not use select * instead of use tablename.fieldName as newname
SQL
select t1.FName as f1,
t2.FName as f2,
t3.FName as f3,
t4.FName as f4

from t1 inner join t2 on t1.col=t2.col 
inner join t3 on  t2.col=t3.col 
inner join t4 on  t3.col=t4.col 
where ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900