需要帮助,我有三张桌子" USERS"," FRIENDS"和"状态"我很难通过" FRIENDS TABLE"
中的条件加入他们 users table
id | name | usercode
--------------------
1 | david | 2WM
2 | Samme | E5N
3 | Awudu | C0Q
4 | John | VX6
5 | Jerem | FG3
Friends Table
id | actor | target
--------------------
1 | E5N | FG3
2 | 2WM | VX6
3 | FG3 | 2WM
4 | C0Q | VX6
5 | FG3 | VX6
Status Table
id | usercode | status
--------------------
1 | E5N | I am busy now
2 | 2WM | The night is falling too fast
3 | FG3 | Good day
4 | C0Q | Very tired, trust me
5 | VX6 | YeLLLLOOO
我希望得到的是所有"用户"来自" USERS TABLE"及其相应的状态"来自"状态表"谁拥有' FG3' as" target"或者' FG3' as"演员"来自" FRIENDS TABLE"
所以结果就像
id | name | usercode | actor | target | status
------------------------------------------------
2 | Samme | E5N | E5N | FG3 | I am busy now
1 | david | 2WM | FG3 | 2WM | The night is falling too fast
5 | John | VX6 | FG3 | VX6 | YeLLLLOOO
请不要错过任何帮助,谢谢。
答案 0 :(得分:1)
首先,将您的用户表与friends表连接,然后将状态表与friends表连接
SELECT friend_user.name,
friend_user.usercode,
Friends.actor,
Friends.target,
friend_status.status
FROM users AS my_user_table
JOIN Friends ON Friends.actor = my_user_table.usercode
JOIN users AS friend_user ON friend_user.usercode = Friends.target
OR friend_user.usercode = Friends.actor
JOIN Status AS friend_status ON friend_status.usercode = friend_user.usercode
WHERE my_user_table.usercode = 'FG3'
答案 1 :(得分:1)
SELECT u.name,
u.usercode,
f.actor,
f.target,
s.status
FROM Users u,
Friends f,
Status s
WHERE ((u.usercode = f.actor AND 'FG3' = f.target)
OR (u.usercode = f.target AND 'FG3' = f.actor))
AND s.usercode = u.usercode