Jump to content

SQL query problem


kney

Recommended Posts

I need to select movies by which preferences the user fills out.

 

By example: user 1 has filled out genre "horror", "thriller"

 

So he needs to get all movies with both those genres, if the moviegenre is "horror", or "thriller" but also if the moviegenre is "horror/thriller"

 

Now the query below only shows the movies with the single values like "horror" but not "horror/thriller" so i need to come up with some kind of LIKE statement in this query to also be able to show the other movies.

 

SELECT *
FROM MOVIE
WHERE MOVIEGENRE
IN (
SELECT GENRENAME
FROM GENRE
LEFT JOIN USERSGENRE
USING (GENREID)
WHERE USERID = 1)

Link to comment
Share on other sites

Hi

 

This suggests an error in how the database is designed. You probably should have another table linking genres to films.

 

Can't really see an elegant way of doing it with how you have it now.

 

If you really must then change the / to a , separating the movie genres and then use the following (and it likely will be inefficient).

 

SELECT * 
FROM MOVIE
WHERE FIND_IN_SET( (
SELECT GENRENAME
FROM GENRE
LEFT JOIN USERSGENRE
USING ( GENREID ) 
WHERE USERID =1 ) , MOVIEGENRE)

 

All the best

 

Keith

Link to comment
Share on other sites

This was the final query which gave the correct result :)

Thanks again!

 

SELECT * 
FROM MOVIE 
WHERE MOVIEID 
IN (
    SELECT MOVIEID
    FROM MOVIEGENRE
    WHERE GENREID IN (
                                    SELECT GENREID
                                    FROM GENRE 
                                    LEFT JOIN USERSGENRE 
                                    USING (GENREID)
                                    WHERE USERID = 1))

Link to comment
Share on other sites

Hi

 

You could use JOINs instead:-

 

SELECT * 
FROM MOVIE 
INNER JOIN MOVIEGENRE ON MOVIE.MOVIEID = MOVIEGENRE.MOVIEID
INNER JOIN GENRE ON MOVIEGENRE.GENREID = GENRE.GENREID
INNER JOIN USERSGENRE GENRE.GENREID = USERSGENRE.GENREID
WHERE USERID = 1

 

If you do not need anything from the GENRE table then you could do without JOINing that one in.

 

You will get duplicate movies this way but a DISTINCT can solve that.

 

All the best

 

Keith

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.