【发布时间】:2023-04-07 17:33:01
【问题描述】:
我有以下三个表:
create table Person (
id int,
popularity int,
primary key (id)
);
create table Movie (
id int,
year int,
primary key (id)
);
create table Person_Movie (
id_person int,
id_movie int,
primary key (id_person, id_movie),
foreign key (id_person) references Person (id_person),
foreign key (id_movie) references Movie (id_movie)
);
我想执行SELECT 来检查哪个人拥有更多popularity 但参与的电影较少。
我该怎么做?
要检查最受欢迎的人,我会执行此操作:
select P.id from Person as P, Movie as M, Person_Movie as PM
where P.id = PM.id and PM.id = M.id
group by (P.id)
order by popularity;
而且,如果我是正确的,为了检查参与较少电影的人,我会执行以下操作:
select P.id from Person as P, Movie as M, Person_Movie as PM
where P.id = PM.id and PM.id = M.id
group by (P.id)
order by count(M.id);
但是,我怎样才能在一个SELECT 中过滤这两种情况?
提前致谢。
编辑
我正在使用 MySQL。
另外,我将提供一个我想要得到的示例:
桌人
id popularity
-------------------
1 50
2 35
3 120
4 45
桌面电影
id year
----------------
1 1999
2 2014
3 1969
4 1977
5 2019
Table Person_Movie
id_person id_movie
--------------------------
1 1
2 4
2 5
3 3
4 1
4 2
所以,给定这个例子,我想得到的输出是:
id_person
----------
3
因为ID为1和3的人都只参加过一部电影,而ID为3的人的热度更高。
【问题讨论】:
-
样本数据、所需结果和适当的数据库标签会有所帮助。此外,学习使用正确、明确、标准、可读的
JOIN语法。 -
了解如何正确使用
JOIN。它已经存在超过 25 年了。