user2721874
user2721874

Reputation:

How to get the records from multiple tables?

Hi I am new to the Database, and i am trying to get the records from the multiple tables, but depending upon there selection following is my tables

 Table1

  Column1      Column2
    1             10
    2             25 
    3             23
    4             15
    5              7


  Table2

  Column1      Column2
    2             15 
    3             13
    5             17

  Table3

  Column1      Column2
    2             45

Resultant Table should have records like

       Column1      Column2
         1             10
         2             45
         3             13
         4             15
         5             17

i am trying but not got the output yet. Any help or the direction to work out this output will be great help.


UPDATE

What i want is get the all rows from table1 then if table2 contains the matching records then it will remove the matching records form the resultset and add the table2 matching records and then same is repeated by table3.

Upvotes: 0

Views: 143

Answers (3)

Jayvee
Jayvee

Reputation: 10875

SELECT t1.column1, COALESCE(t3.column2,t2.column2,t1.column2) 
FROM t1 
LEFT JOIN t2 on t1.column1=t2.column1
LEFT JOIN t3 on t1.column1=t3.column1

Upvotes: 4

Kishore
Kishore

Reputation: 846

Please use the Below Code and Try

select * from table1 where column1 not in  ( select column1 from table2 union select column1 from table3)
union 
select  * from table2 where column1 not in  (select column1 from table3)
union
select  * from table3

Upvotes: 2

vhadalgi
vhadalgi

Reputation: 7189

select x.col1,max(x.col2) from (

select * from #t1
union
select * from #t2
union 
select * from #t3 
)x
group by x.col1

see it in action

Upvotes: 1

Related Questions