我在这里有一个MySQL表Starcraft2brackets:
http://i.stack.imgur.com/welPq.png(重新编辑,以便我可以再次编辑,抱歉第一位编辑)
uId =>引用用户
id =>索引
对手=>在此表中引用id,-2被视为再见。
我正在尝试做的是提出一个sql语句,它将根据行的uId选择一个用户名,并选择其对手的用户名。到目前为止,我提出的最有建设性的是
SELECT u1.username, if(sc2a.opponent = -2, "Bye", u2.username) as username
FROM Starcraft2brackets AS sc2a
LEFT JOIN users AS u1 ON u1.id = sc2a.uId
LEFT JOIN Starcraft2brackets AS sc2b ON sc2a.opponent = sc2b.id
LEFT JOIN users AS u2 ON sc2b.uId = u2.id
这确实给了我正确的用户名,但是它提供了重复的条目,就像这样..
SELECT u1.username, IF( sc2a.opponent = -2, "Bye", u2.username ) AS username
FROM Starcraft2brackets AS sc2a
LEFT JOIN users AS u1 ON u1.id = sc2a.uId
LEFT JOIN Starcraft2brackets AS sc2b ON sc2a.opponent = sc2b.id
LEFT JOIN users AS u2 ON sc2b.uId = u2.id
WHERE u1.username < u2.username
OR sc2a.opponent = -2
这给了我正在寻找的结果集。感谢stackoverflow.com的帮助。
答案 0 :(得分:0)
试试这个:
SELECT u1.username, if(u1.oppponent = -2, "Bye", u2.username) as username
FROM users AS u1
left join users AS u2 on u1.opponent = u2.id
join Starcraft2brackets AS sc2a on u1.id = sc2a.uId
where u1.uid = ?;