I got a users table and movies table, each user could watch more then one movie.
Now I want to find all the users who watched particular movie,
How should my tables looks like in MySql?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To do this, you need 3 tables. The first two are users and movies, they would, in a very basic form, look like this:
Since many users can watch many movies and the same movie can be watched by multiple users, there’s a many-to-many relationship there. To define such a relationship in MySQL, you need a third table called a join table, that knows which user watched which movie (I called this user_movies but you might want to go with a different name):
To know who watched a movie called Spaceballs, you need a query like this: