MySQL 选择流派
我有一个数据库:
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
如何从所有数据库的
流派
中选择独特的流派。 我需要下一个:动作/冒险/剧情/家庭/奇幻/历史/情节剧/惊悚/战争如何观看特定类型的电影?
从
电影
中选择movie_name
,其中流派
喜欢??
但他不仅能带来戏剧,还能带来情节剧。
如何搜索特定类型? 可能是:
从
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
How I can select unique genres from
genres
of all database.
I need the next: Action / Adventure / Drama / Family / Fantasy / History / Melodrama / Thriller / WarHow can I see a movie of a certain genre?
SELECT
movie_name
FROMmovies
WHEREgenre
LIKE ??
But he also can bring not only the drama, but melodrama.
How to make a search on a particular genre?
May be:SELECT
movie_name
FROmmovies
WHEREmovie_name
LIKE%stone%
AND `genres LIKE 'drama'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要在数据库列中存储以逗号分隔的属性列表。
相反,有 3 个表:
那么你的问题就会变得非常非常简单。
Don't store a comma-delimited list of attributes in a database column.
Instead, have 3 tables:
Then your problems become much, much simpler.
您在这里面临的问题是实现 N 到 N 关系要求。
由于电影之间的关系是多对多关系,因此将其存储在数据库模式中的正确方法是在单独的表中维护该关系。
为了便于练习,我们将此表称为“Movie_Genre_Relationship”。如果《虎胆龙威》既是动作片,又是动作片。惊悚片,您应该将其存储在关系表中的两行中,如下所示:
假设有以下电影和流派表:
然后您可以通过执行以下操作来搜索所有动作电影:
希望它有所帮助。
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:
Assuming the following Movie and Genre tables:
Then you can search all Action movies by doing:
Hope it helps.