我想对数据库表执行搜索查询。这是我的表格示例。
movies
--------------------------
ID (INT) | TITLE (VARCHAR)
--------------------------
actors
-------------------------
ID (INT) | NAME (VARCHAR)
-------------------------
directors
-------------------------
ID (INT) | NAME (VARCHAR)
-------------------------
ma
------------------------------------
ID (INT) | MOVIE (INT) | ACTOR (INT)
------------------------------------
md
---------------------------------------
ID (INT) | MOVIE (INT) | DIRECTOR (INT)
---------------------------------------
我想根据某些关键字搜索movies
。title
,actors
。name
,directors
。name
。 ma
表的字段movie
引用movie
。id
和字段actor
,引用actor
。id
等等上。我想按movie
显示结果组。id
。问题是我无法根据表directors
和actors
根据表ma
和md
找出如何执行表格movies
}。对不起语法,如果您认为这个问题值得,可以编辑它。
那么,任何想法?
答案 0 :(得分:0)
这一个你得到了一切
SELECT m.title, a.name AS actor, d.name AS director
FROM movies m
INNER JOIN ma ON (ma.movie_id = m.id)
INNER JOIN actors a ON (a.id = ma.actor_id)
INNER JOIN md ON (md.movie_id = m.id)
INNER JOIN directors d ON (d.id = md.director_id)
ORDER BY m.title
这是你按照电影分组的那个
SELECT m.title, GROUP_CONCAT(a.name) AS actorS, GROUP_CONCAT(d.name) AS directors
FROM movies m
INNER JOIN ma ON (ma.movie_id = m.id)
INNER JOIN actors a ON (a.id = ma.actor_id)
INNER JOIN md ON (md.movie_id = m.id)
INNER JOIN directors d ON (d.id = md.director_id)
GROUP BY m.id
ORDER BY m.title
[编辑]
为了善良'清酒..只需添加WHERE
... 在哪里,像是'关键词%'或者a.name LIKE' KEYWORD%'或者d.name LIKE' KEYWORD%'