Search code examples
phpjoinmatchingnames

Need to match user ids from two fields to their names in another table


I am working on some stuff for my game server and making a custom site ban.

I have a ban table (ban time, reason, banned player, banning admin, etc) The banned player and banning admin field both use player ids which are stored in the other table.

I know how to join tables but I can't figure out how to distinguish between the banned player and the admin, the tables are basically like:

info table:

some info field| more info | more info| player_id | creator_id

info info      | info info | info info|         1 |          2
info info      | info info | info info|         3 |          2
info info      | info info | info info|         5 |          4

players table:

id  | name

1   | john
2   | steve
3   | sally
4   | bob
5   | jack

The player id is the player that was banned and the creator is the admin, all names whether admin or player are all stored in the players table. What I want is to have a page that shows the banned player, the reason for ban, the banning admin and the time


Solution

  • You may try :

    SELECT
      info1,
      info2,
      p1.name AS player_name,
      p2.name AS admin_name
    FROM more_infos mi
    JOIN players p1 ON p1.id = mi.player_id
    JOIN players p2 ON p2.id = mi.creator_id;
    

    see Sql Fiddle with Demo.