我需要你的帮助,这是我的问题:
我有2张 table
Friends
----------------------------------------
friends_club_1 | friends_club_2 | state
1234 4321 1
2345 1234 1
1234 4567 1
4569 1567 1
User Info
-----------------------------------------------
id | name | surname | age |
2345 Jhonny Jenny 24
4567 Linda Minda 23
1567 Stefan Refan 25
1234 me me 23
4569 dani mari 28
4321 Star Gate 24
我希望得到以下结果:
Result
-----------------------------------------------------
my_friends | state | name | surname | age
4321 1 Star Gate 24
2345 1 Jhonny Jenny 24
4567 1 Linda Minda 23
1567 1 Stefan Refan 25
ecc....
我希望得到这个结果,知道我的 ID 1234 希望有帮助,非常感谢
最佳答案
您的结果不正确,“Stefan Refan”不会出现在结果中。
SELECT UI.id AS my_friends,
COALESCE(F1.state,F2.state) AS state,
UI.name, UI.surname, UI.age
FROM [User Info] as UI
LEFT OUTER JOIN [Friends] AS F1 ON (UI.id = F1.friends_club_1)
LEFT OUTER JOIN [Friends] AS F2 ON (UI.id = F2.friends_club_2)
WHERE COALESCE(F1.friends_club_2,F2.friends_club_1) = 1234;
您需要合并 F1.state 和 F2.state 才能找到使用哪一个。
关于php - 使用硬连接查询 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17729004/