0

我有这个查询

SELECT f.id_film, title, year, GROUP_CONCAT(actor SEPARATOR '|') AS 'actors',
GROUP_CONCAT(director SEPARATOR '|') AS 'directors' 
FROM film f JOIN middle m ON f.id_film = m.id_film
JOIN actors a ON m.id_name = a.id_actor 
JOIN directors d ON m.id_name = d.id_director 
WHERE id_category = "com" GROUP BY f.id_film

这应该从表演员和表导演那里收集名字。中间表实际上是两个表之间的“连接器”,并通过一个包含“a”或“d”值的列“type”将一个表与另一个表区分开来。我无法正确地将演员与导演分开,这意味着查询返回所有 id,无论类型如何。我尝试了子查询但没有成功。任何建议将不胜感激。谢谢

4

1 回答 1

0

我认为您在加入演员和导演表时缺少包含中间表的“类型”字段比较,

试试下面:

SELECT f.id_film, title, year, GROUP_CONCAT(actor SEPARATOR '|') AS 'actors',
GROUP_CONCAT(director SEPARATOR '|') AS 'directors' 
FROM film f JOIN middle m ON f.id_film = m.id_film
LEFT JOIN actors a ON m.id_name = a.id_actor AND m.type = 'a'
LEFT JOIN directors d ON m.id_name = d.id_director AND m.type = 'd'
WHERE id_category = "com" GROUP BY f.id_film

@michael 请参阅SqlFiddle,您需要将数据库架构语句和示例数据语句放在左侧,并且您可以在问题中提供链接和预期输出,以便我们可以在示例数据上尝试解决方案。

于 2013-05-09T09:51:16.580 回答