Several fields with one foreign key



  • Hello. Problem is, I have a table. ROOMS:

    id, slot_1, slot_2, slot_3, slot_4, slot_5
    

    And there's a table. USERS:

    uid, name
    

    How do you make a thing like that for everyone? slot_N related USERS?

    The thing is, I'm doing it inside the phpMyAdmin, there's no way I know how to set it up, but it's kind of like the indexes that did everything, but it's like the sample to sign it. LEFT JOIN?



  • May be as follows:

    SELECT
      r.id AS id,
      s1.name AS slot_1,
      s2.name AS slot_2,
      s3.name AS slot_3,
      s4.name AS slot_4,
      s5.name AS slot_5
    FROM
      rooms AS r
    LEFT JOIN
      users AS s1
    ON
      r.slot_1 = s1.ui‌​d
    LEFT JOIN
      users AS s2
    ON
      r.slot_2 = s2.ui‌​d
    LEFT JOIN
      users AS s3
    ON
      r.slot_3 = s3.ui‌​d
    LEFT JOIN
      users AS s4
    ON
      r.slot_4 = s4.ui‌​d
    LEFT JOIN
      users AS s5
    ON
      r.slot_5 = s5.ui‌​d;
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2