mysql获取用户名_mysql-LEFT JOIN 3列获取用户名
我需要連接來自三張不同表格的三列,
捐款表:
+-----------+---------------------+
| record_id | contributor_user_id |
+-----------+---------------------+
| 1 | 2 |
+-----------+---------------------+
| 1 | 5 |
+-----------+---------------------+
會(huì)員表:
+--------------+---------+
| username | user_id |
+--------------+---------+
| Test | 1 |
+--------------+---------+
| Test2 | 5 |
+--------------+---------+
| Test3 | 6 |
+--------------+---------+
記錄表:
+---------+-----------+
| user_id | record_id |
+---------+-----------+
| 28 | 1 |
+---------+-----------+
對于我需要返回的是用于顯示記錄所有者的用戶名和user_id.另外,顯示用戶名和user_id,但是可以是多個(gè)(超過1個(gè)用戶).我已經(jīng)試過了:
SELECT usr.username,
usr.user_id,
rec.record_id,
contrib.record_id,
contrib.contributor_user_id
FROM
(
records rec
INNER JOIN members usr ON rec.user_id = usr.user_id
# this returns records as NULL
LEFT OUTER JOIN contributions contrib ON rec.record_id = contrib.record_id AND contrib.contributor_user_id = usr.user_id
# this works, but I need the username to be displayed too
LEFT OUTER JOIN contributions contrib ON rec.record_id = contrib.record_id
)
WHERE rec.record_id = 1
總結(jié)
以上是生活随笔為你收集整理的mysql获取用户名_mysql-LEFT JOIN 3列获取用户名的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql data目录 说明_mysq
- 下一篇: python selenium 弹窗获取