0

Hi I have the following tables and columns.

movie: ID, title

person: ID, name

involved: personID, movieID

I need to answer the question:

"Which movies have either John Travolta or Uma Thurman, but not both starred in?"

I couldn't figure out how to do this without creating new tables, so I made 2 new tables. And tried to do the full outer join on, where you dont get intersecting results. I found out that you can't do full outer joins in mysql but had to do a left join, unioned with a right join. I tried this but don't get the results I wanted at all. I have been stuck for a while now. Can anyone point me in the right direction?

This is what I have so far.

DROP TABLE IF EXISTS Umatable;
DROP TABLE IF EXISTS Johntable;

CREATE TABLE Umatable(title VARCHAR(500));
CREATE TABLE Johntable(title VARCHAR(500));

INSERT INTO Umatable
SELECT m.title
FROM movie m, person p, involved i
WHERE p.name = "Uma Thurman"
AND p.id = i.personid
AND m.id = i.movieiD;

INSERT INTO Johntable
SELECT m.title
FROM movie m, person p, involved i
WHERE p.name = "John Travolta"
AND p.id = i.personid
AND m.id = i.movieiD;

SELECT * 
FROM Umatable
LEFT JOIN Johntable ON Umatable.title = Johntable.title
WHERE Johntable.title IS NULL OR Umatable.title IS NULL
UNION
SELECT *
FROM Umatable 
RIGHT JOIN Johntable ON Umatable.title = Johntable.title
WHERE Johntable.title IS NULL OR Umatable.title IS NULL
Copperström
  • 359
  • 2
  • 6

2 Answers2

2

I would do this using aggregation and having:

select i.movieId
from involved i join
     person p
     on p.id = i.personId
group by i.movieId
having sum(p.name in ('John Travolta', 'Uma Thurman')) = 1;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

A count(*) inside a correlated subquery will work:

select *
  from movie m
 where 1 = (select count(*)
              from involved i
              join person p
                on p.ID = i.personID
               and p.name IN ('John Travolta', 'Uma Thurman')
             where i.movieID = m.ID)

SQLFiddle Demo

sstan
  • 35,425
  • 6
  • 48
  • 66