How to join multiple tables in sql

南笙酒味 提交于 2019-12-13 08:35:07

问题


Hi friends I have 3 table topups, withdraws and transfers these 3 tables belongs to the user table. I have to find all the records which belongs to the users. I tried with inner join as follows:-

SELECT * FROM users u
INNER JOIN topups t
  ON u.id = t.user_id
INNER JOIN withdraws w
  ON u.id = w.user_id
INNER JOIN transfers tf
  ON u.id = tf.user_id

But this query returns only the common records between the 3 tables. i have to find all those records which belongs to the user for each table.

Suppose i have 2 records in topups which belongs to user id 1, 3 records in withdraws which belongs to user id 2 and 5 records in transfers which belongs to user id 3 so i should get the total 10 records.

sample data:-

topups

+--------+---------+---------+
| amount | result  | user_id |
+--------+---------+---------+
|     10 | success |       1 |
|     20 | failed  |       2 |
+--------+---------+---------+

withdraws

+---------+----------+
|w_amount |  user_id |
+---------+----------+
|     10  |        1 |
|     20  |        2 |
|     30  |       10 |
+---------+----------+

Transfers

+--------+--------+---------+
| method | amount | user_id |
+--------+--------+---------+
| abc    |     10 |       3 |
| xyz    |     20 |       4 |
+--------+--------+---------+

users

+----+---------+--------+
| id |  f_name | l_name |
+----+---------+--------+
|  1 |    abc  |    xyz |
|  2 |    abc  |    xyz |
|  3 |    abc  |    xyz |
|  4 |    abc  |    xyz |
|  5 |    abc  |    xyz |
|  6 |    abc  |    xyz |
+----+---------+--------+

Expected output

+--------+---------+---------+----------+---------+
| amount | result  | user_id | w_amount |  method |
+--------+---------+---------+----------+---------+
|     10 | success |       1 |          |         |
|     20 | failed  |       2 |          |         |
|        |         |       1 |       10 |         |
|        |         |       2 |       20 |         |
|        |         |       3 |          | abc     |
|        |         |       4 |          | xyz     |
+--------+---------+---------+----------+---------+

Please help Thanks in advance.


回答1:


Left joining them to the users and to a number works for this.

SELECT 
 tup.amount, tup.result, 
 usr.id as user_id, 
 wd.w_amount, 
 trans.method
FROM users usr
CROSS JOIN (SELECT generate_series n FROM generate_series(1, 3)) AS nr
LEFT JOIN topups tup ON tup.user_id = usr.id AND nr.n = 1
LEFT JOIN withdraws wd ON wd.user_id = usr.id AND nr.n = 2
LEFT JOIN transfers trans ON trans.user_id = usr.id AND nr.n = 3
WHERE (tup.user_id IS NOT NULL OR wd.user_id IS NOT NULL OR trans.user_id IS NOT NULL)
ORDER BY tup.user_id, wd.user_id, trans.user_id

Test it here

Extra:

A variation based on the comments here




回答2:


You can use left joins instead of inner joins:

SELECT * FROM users u
LEFT JOIN topups t
  ON u.id = t.user_id
LEFT JOIN withdraws w
  ON u.id = w.user_id
LEFT JOIN transfers tf
  ON u.id = tf.user_id


来源:https://stackoverflow.com/questions/53794078/how-to-join-multiple-tables-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!