需要帮助,我有三个表“用户”、“朋友”和“状态”我很难按条件加入到“朋友表”中

 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

我想得到的是“用户表”中的所有“用户”和“状态表”中相应的“状态”,其中“FG3”是“目标”或“FG3”是“朋友表”中的“参与者”
所以结果会是
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

请帮忙,谢谢。

最佳答案

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

关于php - 在三个表上的MySQL查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27955280/

10-12 13:17