1

Keeping this clean and to the point:

I have two MySQL tables, users (id, nick) and bans (id, banned, bannedBy, length). I want to display in a table a list of bans, but instead of displaying the banned ID and the bannedBy ID, I want to display their nick. I can use an JOIN to get the nick of one of them, in something like this:

SELECT bans.id,bans.banned,bans.bannedBy,bans.length,users.nick
FROM bans
JOIN users ON users.id=bans.banned

But then I can't get the bannedBy's nick, and vice verca.

I hope I was clear, thanks in advance for any help.

4

2 に答える 2

2

users異なるキーで、テーブルに 2 回参加する必要があります。

SELECT
    bans.id,
    bans.banned,
    bans.bannedBy,
    bans.length,
    u.nick as 'banedNick',
    u2.nick as 'bannedByNick'
FROM
    bans
JOIN 
    users u ON users.id = bans.banned
JOIN
    users u2 ON users.id = bans.bannedBy
于 2013-03-10T09:13:42.890 に答える
1

次の 2 つの結合を使用できます。

SELECT bans.id,bans.banned,b.nick as bannedBy,bans.length,u.nick
FROM bans
JOIN users u ON u.users.id=bans.banned
JOIN users b ON b.users.id=bans.bannedBy
于 2013-03-10T09:14:24.030 に答える