简体   繁体   中英

mysql - join two tables to get data from same tables other row

I have two tables named users and requests structured as following:

users:

+----+----------+------------------------+
| id | name     | email                  |
+----+----------+------------------------+
| 1  | super    | super@test.com         |
+----+----------+------------------------+
| 2  | david    | david@test.com         |
+----+----------+------------------------+
| 3  | smith    | smith@test.com         |
+----+----------+------------------------+
| 4  | philip   | philip@test.com        |
+----+----------+------------------------+

requests:

+----+---------+----------------------+
| id | inviter | email                |
+----+---------+----------------------+
| 1  | 1       | david@test.com       |
+----+---------+----------------------+
| 2  | 2       | smith@test.com       |
+----+---------+----------------------+
| 3  | 2       | philip@test.com      |
+----+---------+----------------------+

Now I want to join this two table to get data as following:

+----+----------+------------------------+-----------+
| id | name     | email                  | inviter   |
+----+----------+------------------------+-----------+
| 1  | super    | super@test.com         | null      |
+----+----------+------------------------+-----------+
| 2  | david    | david@test.com         | super     |
+----+----------+------------------------+-----------+
| 3  | smith    | smith@test.com         | david     |
+----+----------+------------------------+-----------+
| 4  | philip   | philip@test.com        | david     |
+----+----------+------------------------+-----------+

Two tables will be joined on email attribute. And inviter field in resulting table is users name for corresponding id stored in requests table under inviter attribute.

I have wrote this query

select users.id as id, name, users.email as email, name, inviter
from users
left join requests on users.email = requests.email

which produce following result

+----+--------+-----------------+--------+---------+
| id | name   | email           | name   | inviter |
+----+--------+-----------------+--------+---------+
|  2 | david  | david@test.com  | david  |       1 |
|  3 | smith  | smith@test.com  | smith  |       2 |
|  4 | philip | philip@test.com | philip |       2 |
|  1 | super  | super@test.com  | super  |    NULL |
+----+--------+-----------------+--------+---------+

Can anyone help me to write a query to get data as intended?

You must join users to requests and users again to get the name of the inviter :

select u.*, u2.name inviter
from users u 
left join requests r on r.email = u.email
left join users u2 on u2.id = r.inviter
order by u.id

See the demo .
Results:

| id  | name   | email           | inviter |
| --- | ------ | --------------- | ------- |
| 1   | super  | super@test.com  |         |
| 2   | david  | david@test.com  | super   |
| 3   | smith  | smith@test.com  | david   |
| 4   | philip | philip@test.com | david   |

You need to use left join twice with user table to get this, refer below db fiddle and query

SELECT US.*, US1.name 
FROM users AS US 
LEFT JOIN requests AS RQ ON RQ.email = US.email
LEFT JOIN users AS US1 ON US1.id = RQ.inviter

DB FIDDLE LINK: HERE

You need to join the table users for a second time to get the name of the inviter. For example:

select
  u.*,
  u2.name as inviter
from users u
left join requests r on r.email. = u.email
left join users u2 on u2.id = r.inviter
order by u.id

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