简体   繁体   中英

sql to extract same columns from 3 different tables

Hi i have 3 tables i want to extract same columns from 3 tables is this better way to write Select query.

select * from 
(
select col1,
select id1 from testid1 where name=pnrtable1.name,
col3 from table1  
union all  
select coltab1,
select newid2 from testid2 where name=pnrtable2.name,
coltab3 from table2  
union all  
select namecol1,
select id3 from testid3 where name=pnrtable3.name,
namecol3 from table3  
)

Not sure but I think you are after something like this....

select * from 
(
select t1.col1, t2.id1 , t1.col3 from table1  t1 
                                 INNER JOIN testid1 t2 ON t1.name = t2.name
union all  
select t1.coltab1, t2.newid2, t1.coltab3 from testid2 t1 
                                         INNER JOIN table2 t2 ON t1.name=t2.name
union all  
select t1.namecol1, t2.id3, t1.namecol3 from testid3 t1 
                                       INNER JOIN table3 t2 ON t1.name=t2.name
) A

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM