MySQL选择流派问题(PHP) [英] MySQL select genres issue (php)

查看:48
本文介绍了MySQL选择流派问题(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库:

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)如何从所有数据库的genres中选择独特的流派. 我需要下一个:动作/冒险/戏剧/家庭/幻想/历史/情节剧/惊悚/战争

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)我如何观看某种类型的电影?

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.

3)如何搜索特定类型? 可能是:

3) 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'.

推荐答案

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

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

相反,有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

然后您的问题变得简单得多.

Then your problems become much, much simpler.

这篇关于MySQL选择流派问题(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆