嵌套选择或连接查询?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了嵌套选择或连接查询?相关的知识,希望对你有一定的参考价值。

我是sql的新手,所以这可能有一个非常基本的答案,但要回答的问题如下.....

哪部电影在表演中收入最少?在结果中包含电影名称和电影名称。

电影名称和电影名称分为两部不同的电影和电影。收入在绩效表中。我无法弄清楚如何完成此查询。这是我到目前为止所得到的,但它在第3行第7列中出现错误。

select cinema_no,film_no
from CINEMA, film
where takings ( select min(takings)
from performance);
答案

因为有oracle的多个标签,我忽略了标签mysql(你应该摆脱的标签。例如,请你决定使用哪个DBMS,顺便说一下我已经删除了无关的oracle-sqldeveloper)。

看来你需要这样一个select语句(更喜欢使用现代ANSI-92 JOIN语法,易于维护和理解),通过降序sumrow_number函数的贡献排序为:

SELECT Name, Sum_Takings 
  FROM
  (
   SELECT f.Name, sum(p.Takings) Sum_Takings,
          row_number() over (ORDER BY sum(p.Takings)) as rn
     FROM Film f 
     LEFT JOIN Cinema c ON f.Cinema_ID = c.ID
     LEFT JOIN Performance p ON f.ID = p.id_film
    GROUP BY f.Name
   )
  WHERE rn = 1;

添加了DDL语句,如下所示:

SQL>  CREATE TABLE Cinema (
  2     ID     integer PRIMARY KEY NOT NULL,
  3     Title  varchar2(100) NOT NULL
  4   );

Table created

SQL>  CREATE TABLE Film (
  2     ID   integer PRIMARY KEY NOT NULL,
  3     Name varchar2(100) NOT NULL,
  4     Cinema_ID integer
  5       CONSTRAINT fk_Cinema_ID REFERENCES Cinema(ID)
  6   );

Table created

SQL>   CREATE TABLE Performance (
  2     ID      integer PRIMARY KEY NOT NULL,
  3     ID_Film integer
  4          CONSTRAINT fk_Film_ID REFERENCES Film(ID),
  5     Takings integer
  6   );

Table created

SQL>  INSERT ALL
  2         INTO Cinema(ID,Title) VALUES(1,'NiteHawk')
  3         INTO Cinema(ID,Title) VALUES(2,'Symphony Space')
  4         INTO Cinema(ID,Title) VALUES(3,'The Ziegfeld')
  5         INTO Cinema(ID,Title) VALUES(4,'Cinema Village')
  6       SELECT * FROM dual;

4 rows inserted

SQL>  INSERT ALL
  2         INTO Film(ID,Name,Cinema_ID) VALUES(1,'Citizen Kane',1)
  3         INTO Film(ID,Name,Cinema_ID) VALUES(2,'Titanic',2)
  4         INTO Film(ID,Name,Cinema_ID) VALUES(3,'Brave Heart',4)
  5         INTO Film(ID,Name,Cinema_ID) VALUES(4,'Dumb and Dummer',3)
  6         INTO Film(ID,Name,Cinema_ID) VALUES(5,'How To Train Your Dragon',2)
  7         INTO Film(ID,Name,Cinema_ID) VALUES(6,'Beetle Juice',3)
  8       SELECT * FROM dual;

6 rows inserted

SQL>  INSERT ALL
  2         INTO Performance VALUES(1,1,15)
  3         INTO Performance VALUES(2,1,4)
  4         INTO Performance VALUES(3,2,10)
  5         INTO Performance VALUES(4,3,1)
  6         INTO Performance VALUES(5,4,5)
  7         INTO Performance VALUES(6,3,3)
  8         INTO Performance VALUES(7,2,7)
  9         INTO Performance VALUES(8,5,7)
 10         INTO Performance VALUES(9,6,6)
 11       SELECT * FROM dual;

9 rows inserted

SQL> commit;

Commit complete

SQL> SELECT Name, Sum_Takings
  2    FROM
  3    (
  4     SELECT f.Name, sum(p.Takings) Sum_Takings,
  5            row_number() over (ORDER BY sum(p.Takings)) as rn
  6       FROM Film f
  7       LEFT JOIN Cinema c ON f.Cinema_ID = c.ID
  8       LEFT JOIN Performance p ON f.ID = p.id_film
  9      GROUP BY f.Name
 10     )
 11    WHERE rn = 1
 12  ;

NAME                                                                  SUM_TAKINGS
--------------------------------------------------------------------- -----------
Brave Heart                                                                     4

dbfiddle.uk demo

另一答案

因为它涉及到性能表的分组,你可以通过使用两个查询来获得它,以获得最低性能收益的cinemaID和filmID,另一个使用收集的ID获取电影名称和电影名称。

SELECT CinemaID,PerformanceID中的FilmID,其中PerformanceTaking =(从Performance中选择Min(PerformanceTaking));

SELECT CinemaName,来自Cinema的FilmName,Film,其中CinemaID = 1,FilmID = 1;

另一答案

这将工作:

select * from (select c.cinema_name,f.film_name
from cinema c, film f,performance p,
rank() over (partition by c.cinema order by sum(p.takings)) as rank
where
c.ID=f.cinema_id and
f,id=p.id
group by f.film_name) where rank=1
;

以上是关于嵌套选择或连接查询?的主要内容,如果未能解决你的问题,请参考以下文章

在 Rails 上为连接、限制、选择等(不是条件)的 SQL 片段安全地转义字符串

嵌套查询与连接查询的区别是啥

嵌套选择的内部连接 ​​- 多次使用同一列时

嵌套片段数据在中继中始终相同

条件片段和导航重用

从嵌套 SQL 查询中选择 TOP 或 GROUP BY 排名数据