dude
dude

Reputation: 4962

Joining three different tables based on one column name

table A
amount user_id
100     abc
200     cdf
300     def

table B
Idno    user_id
10        abc
202       def

table C
Idno    user_id
498      cdf

final output

Idno    user_id     amount
10        abc       100
202       def       200
498       cdf       300

I Know to join two tables

select A.amount,B.Idno,B.user_id from B inner join A on A.user_id=B.user_id;

but my question is how to display all the amount of user_id in both the table as shown in the final output

Upvotes: 0

Views: 190

Answers (1)

Devart
Devart

Reputation: 121912

It this what you want?

SELECT a.amount, t.Idno, t.user_id FROM table_a a
  JOIN (
    SELECT Idno, user_id FROM table_b
      UNION ALL
    SELECT Idno, user_id FROM table_c
  ) t
  ON a.user_id = t.user_id

Upvotes: 2

Related Questions