0

我想对我的数据库表执行搜索查询。这是我的表格示例。

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引用的字段movieidactor引用的字段actorid等等。我想按 . 显示结果组movieid. 问题是我不知道如何基于 tabledirectorsactors,基于 tablemamd引用 table来执行movies。对不起语法,如果你认为这个问题值得,你可以编辑它。

那么,有什么想法吗?

4

1 回答 1

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

... WHERE m.title LIKE 'KEYWORD%' OR a.name LIKE 'KEYWORD%' OR d.name LIKE 'KEYWORD%'

于 2013-05-24T05:05:18.017 回答