MySQL 选择流派

发布于 2024-10-25 13:21:59 字数 878 浏览 0 评论 0原文

我有一个数据库:

id       |                movie_name              |           genres
1        |                 Die Hard               |        Action, Thriller
2        |                Gladiator               | Adventure, Action, Drama, History
3        |  Harry Potter and the Sorcerers Stone  |    Fantasy, Adventure, Family
4        |               Pearl Harbor             |      Action, Melodrama, War
  1. 如何从所有数据库的流派中选择独特的流派。 我需要下一个:动作/冒险/剧情/家庭/奇幻/历史/情节剧/惊悚/战争

  2. 如何观看特定类型的电影?

    电影中选择movie_name,其中流派喜欢??

但他不仅能带来戏剧,还能带来情节剧。

  1. 如何搜索特定类型? 可能是:

    movies 中选择 movie_name,其中 movie_name LIKE %stone% AND `genres LIKE 'drama'。< /p>

I have a database:

id       |                movie_name              |           genres
1        |                 Die Hard               |        Action, Thriller
2        |                Gladiator               | Adventure, Action, Drama, History
3        |  Harry Potter and the Sorcerers Stone  |    Fantasy, Adventure, Family
4        |               Pearl Harbor             |      Action, Melodrama, War
  1. How I can select unique genres from genres of all database.
    I need the next: Action / Adventure / Drama / Family / Fantasy / History / Melodrama / Thriller / War

  2. How can I see a movie of a certain genre?

    SELECT movie_name FROM movies WHERE genre LIKE ??

But he also can bring not only the drama, but melodrama.

  1. How to make a search on a particular genre?
    May be:

    SELECT movie_name FROm movies WHERE movie_name LIKE %stone% AND `genres LIKE 'drama'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音栖息无 2024-11-01 13:21:59

不要在数据库列中存储以逗号分隔的属性列表。

相反,有 3 个表:

Movies (id, movie_name)

id | movie_name
---+--------------------------------------
 1 | Die Hard
 2 | Gladiator
 3 | Harry Potter and the Sorcerers Stone
 4 | Pearl Harbor

Genres (id, genre_name)

id | genre_name
---+------------
 1 | Action
 2 | Thriller
 3 | Adventure
 4 | Drama
 5 | History
 6 | Fantasy
 7 | Family
 8 | Melodrama
 9 | War

MovieGenre (movie, genre)

Movie | Genre
------+-------
    1 | 1
    1 | 2
    2 | 1
    2 | 3
    2 | 4
    2 | 5
    3 | 3
    3 | 6
    3 | 7
    4 | 1
    4 | 8
    4 | 9

那么你的问题就会变得非常非常简单。

Don't store a comma-delimited list of attributes in a database column.

Instead, have 3 tables:

Movies (id, movie_name)

id | movie_name
---+--------------------------------------
 1 | Die Hard
 2 | Gladiator
 3 | Harry Potter and the Sorcerers Stone
 4 | Pearl Harbor

Genres (id, genre_name)

id | genre_name
---+------------
 1 | Action
 2 | Thriller
 3 | Adventure
 4 | Drama
 5 | History
 6 | Fantasy
 7 | Family
 8 | Melodrama
 9 | War

MovieGenre (movie, genre)

Movie | Genre
------+-------
    1 | 1
    1 | 2
    2 | 1
    2 | 3
    2 | 4
    2 | 5
    3 | 3
    3 | 6
    3 | 7
    4 | 1
    4 | 8
    4 | 9

Then your problems become much, much simpler.

香草可樂 2024-11-01 13:21:59

您在这里面临的问题是实现 N 到 N 关系要求。

由于电影之间的关系是多对多关系,因此将其存储在数据库模式中的正确方法是在单独的表中维护该关系。

为了便于练习,我们将此表称为“Movie_Genre_Relationship”。如果《虎胆龙威》既是动作片,又是动作片。惊悚片,您应该将其存储在关系表中的两行中,如下所示:

Movie_Genre_Relationship
Movie_id | Genre_id
1        | 1
1        | 2

假设有以下电影和流派表:

Movies
Movie_id | Movie_Name
1        | Die Hard

Genres
Genre_id | Genre_Name
1        | Action
2        | Thriller

然后您可以通过执行以下操作来搜索所有动作电影:

select * from Movies m
inner join Movies_Genres_Relationship r on m.movie_id = r.movie_id
where r.genre_id = 1

希望它有所帮助。

The problem you're facing here is implementing an N to N relationship requirement.

Since the relation between movies is a Many to Many relationship, the proper way of storing this in a database schema is to maintain the relationship in a separate table.

Let's call this table "Movie_Genre_Relationship" for the sake of this exercise. If Die Hard is both an Action & Thriller movie, you should store it with two rows in the relationship table as such:

Movie_Genre_Relationship
Movie_id | Genre_id
1        | 1
1        | 2

Assuming the following Movie and Genre tables:

Movies
Movie_id | Movie_Name
1        | Die Hard

Genres
Genre_id | Genre_Name
1        | Action
2        | Thriller

Then you can search all Action movies by doing:

select * from Movies m
inner join Movies_Genres_Relationship r on m.movie_id = r.movie_id
where r.genre_id = 1

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文