0

I am trying to figure out a way to find shared credits by two people in a movie database, for example:

table: 'credits'
columns: 'id','movie','person'

My other problem is a person might have multiple credits for the same movie, so how do I filter out duplicates? Any help would be appreciated. The following is what I have so far. Am I on the right track?

SELECT DISTINCT movie
FROM credits
WHERE person = 'condition1' OR person = 'condition2'
GROUP BY movie
HAVING COUNT(*)=2
8
  • what output are you getting on execution of your query? Commented Apr 29, 2011 at 10:45
  • DISTINCT is redundant if you GROUP BY. About duplicates, Why don't you set a UNIQUE KEY on movie + person? Commented Apr 29, 2011 at 10:46
  • @Capsule Coz it's not unique by the OP's description :) Commented Apr 29, 2011 at 10:51
  • @Emmerman well, according to the table description (no other fields than id/movie/person), movie+person should be unique (i'm talking about one unique key on both columns at once, not 2 unique keys). Commented Apr 29, 2011 at 10:55
  • @Capsule I think there's more columns (something like role - editor, actor, producer etc) Commented Apr 29, 2011 at 10:59

4 Answers 4

1
SELECT DISTINCT c1.movie
FROM credits AS c1
JOIN credits AS c2 on (c1.movie = c2.movie)
WHERE c1.person = 'john'
AND c2.person = 'kate'
Sign up to request clarification or add additional context in comments.

3 Comments

Speaking of speed ... moving the WHERE conditions to JOIN would be faster, but it's a correct answer, nonetheless.
Yes, this worked great but as Narf recommended the JOIN is faster like GordonM posted below. Didn't realize you could join a table on itself... newb ->
@Stephen you haven't timed it, it's an identical join.
1

This should work:

SELECT DISTINCT movie
FROM (
      SELECT movie, COUNT(person) AS contributors
      FROM credits
      WHERE person IN('person1','person2')
      GROUP BY movie, person
      HAVING contributors>1
     ) t1

4 Comments

-1 very slow query, group by movie, person should be group by person then select distinct movie can be just select movie. Of course the count will still make it slow.
GROUP BY person would give incorrect results when there's a person that has two contributions to the same movie. It might be slow for a table that has millions of rows, but I never said it was fast and you're just trying to find an excuse to give negative votes for everybody that's trying to help(except for yourself of course ...), which is pure trolling.
looking at the answers, I see your point. Speed was indeed not a requirement, and your answer does give the correct result.
@Johan, I don't really care about the votes, a single one won't ruin my "reputation" or anything. I never tested it anyways ... so it might not be correct too. I just wanted to make my point and since you get it ... it's fine by me.
0
select distinct cr1.movie
from credits as cr1
join credits as cr2
using (movie)
where cr1.person = {person 1 goes here}
and cr2.person = {person 2 goes here}

2 Comments

-1 won't work because you are joining on the unique id, giving an empty get, because a credit with the same id cannot have 2 people.
THANK YOU. This works great and seems to be the fastest solution. You rock my face off. I peed my pants just now. Literally.
-1

You no need this HAVING COUNT(*)=2. It gives you only movies with two matches (one person with two credits or with both condition1 and condition2). More than that - you no need group by at all when you using distinct (in this case).

SELECT DISTINCT movie
FROM credits
WHERE person = 'condition1' OR person = 'condition2'

1 Comment

@Johan Agreed. Miss shared in question (still not good with english)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.