Best method for storing a list of user IDs

后端 未结 2 1630
说谎
说谎 2020-12-15 14:25

I\'m working on a PHP/MySQL rating system right now. For the user to be able to rate the user has to log in. Each user has a unique \"UID\". There will be multiple rating in

相关标签:
2条回答
  • 2020-12-15 14:30

    It is inefficient. What you have here in relational terms is a many-to-many relationship between users and games. A user can vote on many games. A game can be voted on by many users. The solution for this is to have a join table:

    USERS (uid, name, ...)
    GAMES (gid, name, ...)
    VOTES (id, uid, gid, ...)
    

    Where uid and gid are foreign keys back to their respective tables.

    If someone votes, insert a record in VOTES.

    To get a list of votes for a game:

    $get = mysql_query("SELECT * FROM votes WHERE gid = $game_id");
    ...
    

    To get a list of the user's votes:

    $get = mysql_query("SELECT * FROM votes WHERE uid = $user_id");
    ...
    

    and so on.

    Don't join an array and store it in a single column. You're right to avoid that.

    0 讨论(0)
  • 2020-12-15 14:49

    In a normalized database, you should store it in a junction table:

    UserRatings
    -------------
    RatingID int
    UserID int
    UserVote int
    

    I don't know what are your queries. Depending on the application, this might not have the acceptable performance. However, in either case, I suggest you go with the normalized approach, benchmark, and denormalize only if appropriate.

    0 讨论(0)
提交回复
热议问题