1

So I'm making a movie website and in my database table, I've got a column called Genre. In this I have listed the genres like this; Horror, Action.

Example Table

+----+---------+--------+
| id | Genre            |
+----+---------+--------+
|  1 | Action           |
|  2 | Horror, Action   |
|  3 | Horror           |
|  4 | Action           |
|  5 | Romance, Drama   |
|  6 | Horror, Drama    |
+----+---------+--------+

So if I were to do a query to get films with a Horror genre, it would return the ID's 2,3 & 6. How would I go about structuring a query to do this?

Thanks.

3
  • Did you make an attempt to write any sql code?
    – rs.
    May 2, 2014 at 20:39
  • 1
    What sql database platform are you using? Is there any way to change this format to a one to many relationship instead of storing comma delimited strings in your db?
    – crthompson
    May 2, 2014 at 20:39
  • 1
    What do you think of my answer? does it help?
    – crthompson
    May 2, 2014 at 23:27

2 Answers 2

2

The structure of your database is no good. Read up on database normalization to understand why this is. I would go with a structure like this:

Movie:
Id
Name

Movie_Genres
Movie_Id
Genre_Id

Genres:
Id
Name

You can then do a query like this:

SELECT m.name 
FROM Movie as m
INNER JOIN Movie_Genres as mg
  ON mg.Movie_id = m.id
WHERE mg.Genre_id = {Horror genre Id}

If you don't structure your DB like this you will run into a lot of problems down the road.

8
  • Or even partition to create his comma delimited strings if he so desires.
    – crthompson
    May 2, 2014 at 20:43
  • I'm aware of data normalization, but seeing as this is the first time I've used SQL in a web development environment and my SQL isn't great I thought I'd ask the question! So there would be a one to many relationship between these tables yes? I think I will follow your advice and create an additional instead of the one column.
    – horHAY
    May 2, 2014 at 20:47
  • @horHAY, there is an answer to your exact question, but Abe has it right here. Best practices would be to change your structure and not store comma delimited strings in your db.
    – crthompson
    May 2, 2014 at 20:50
  • @paqogomez Yeah this is for a university assignment, I'm not thrilled about it so really I want to get it done with. Could you provide an example of a query using partitioning?
    – horHAY
    May 2, 2014 at 20:51
  • @horHAY Partitioning would be used if you have a normalized db and want to output a comma delimited string. Is that what you're asking for? Are you using SQL Server?
    – crthompson
    May 2, 2014 at 20:53
1

Continuing off of @Abe's excellent answer. If you want to build a comma delimited string based on @Abe's normalized 3 table structure it would look like this:

SELECT  m.Name, 
        STUFF(( SELECT  ', ' + g.Name 
                FROM Movie_Genres as mg
                INNER JOIN Genres AS g on mg.Genre_id = g.Id and mg.Movie_id = m.id
                FOR XML PATH('')
            ), 1, 2, '') 
FROM Movie AS m
GROUP BY m.Name, m.Id; 

Here's a Fiddle

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.