通过联接表在原始sql中查找
给出了3张表格。我需要构建SQL查询来找到两个演员,他们组合在一起最多,并列出标题 那些电影中的一部。按字母顺序排序 表演者通过联接表在原始sql中查找,sql,Sql,给出了3张表格。我需要构建SQL查询来找到两个演员,他们组合在一起最多,并列出标题 那些电影中的一部。按字母顺序排序 表演者 Column | Type | Modifiers ------------+-----------------------------+---------- actor_id | smallint | not null film_id | smallint
Column | Type | Modifiers
------------+-----------------------------+----------
actor_id | smallint | not null
film_id | smallint | not null
。。。
表演者
Column | Type | Modifiers
------------+-----------------------------+----------
actor_id | integer | not null
first_name | character varying(45) | not null
last_name | character varying(45) | not null
。。。
桌上贴膜
Column | Type | Modifiers
------------+-----------------------------+----------
film_id | integer | not null
title | character varying(255) | not null
。。。
所需输出:
first_actor | second_actor | title
------------+--------------+--------------------
John Doe | Jane Doe | The Best Movie Ever
…输入数据和预期结果将很有帮助。另外,请标记您正在使用的数据库。您可以尝试以下代码,看看它是否有效:
SELECT (a.first_name || ' ' || a.last_name) AS First_Actor,
(b.first_name || ' ' || b.last_name) AS Second_Actor,
c.title
FROM actor a
JOIN
(SELECT a.actor_id AS first_actor,
b.actor_id AS second_actor,
a.film_id
FROM film_actor a
JOIN film_actor b ON a.film_id = b.film_id
AND a.actor_id < b.actor_id) ab ON a.actor_id = ab.first_actor
JOIN actor b ON b.actor_id = ab.second_actor
JOIN film c ON c.film_id = ab.film_id
样本表数据和预期结果将有所帮助@你能解释一下我需要补充什么吗?我在postYes的底部提供了预期的结果,但是缺少匹配的样本数据。每张桌子几行通常都可以。@jarlh如何添加它?@jarlh找到了一个很好的方法,非常感谢!现在只剩下为两个演员添加选择条件,这两个演员组合最多。只列出他们的电影SMB你能帮我完成最后一部分吗?