Jump to content

SQL join question


akufen

Recommended Posts

Hi

I have two tables, person and rating. person contains only an id while rating contains id, person_id, value, and movie_id.

Now what I want to do is to query for all the people who have rated two diffferent movies, say with id's 45 and 56.

I know how to do this for a single movie at a time with the following query:

SELECT person.id AS personId FROM person, rating WHERE person.id = rating.person_id AND rating.value = 5 AND rating.movie_id = 45.

But how do I in a single query search for people who have rated both movies 45 and 56 with the value 5?

Thanks!
Link to comment
Share on other sites

[code]SELECT person.id AS personID FROM person INNER JOIN rating ON person.id=rating.person_id WHERE (rating.movie_id=45 OR rating.movie_id=56) AND rating.value=5[/code]

That will return multiple rows for one person, though... Off the top of my head I'm not quite sure how to combine those into just one row.  You might try changint the above WHERE clause to something like
[code]
EXISTS (SELECT COUNT(rating.person_id) WHERE (rating.movie_id=45 OR rating.movie_id=56) AND rating.value=5)[/code]

Something like that ought to do it, I think.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.