如何从嵌套查询中检查返回的元组中的每个条目是否相同?
嘿,我是SQL的新手,我似乎找不到一种解决问题的方法。我有以下实体,
passengers (`id` INT, `name` VARCHAR(45), `surname` VARCHAR(45),
`year_of_birth` INT)
flights (`id` INT, `routes_id` INT, `date` DATE, `airplanes_id` INT)
flights_has_passengers (`flights_id` INT, `passengers_id` INT)
airplanes (`id` INT, `number` VARCHAR(45), `manufacturer`
VARCHAR(45), `model` VARCHAR(45)).
我想制作一个脚本,找到所有乘客的名字,至少在飞行中,如果登上了多个飞机,那些飞机必须是使用同一架飞机进行的。一个所有的声明,要检查典型中的每个条目是否相同。
编辑:我认为以下解决方案,但我不知道它是否正确:
select p1.name, p1.surname
from passengers p1, airplanes a1, flights_has_passengers, flights
where
p1.id = flights_has_passengers.passengers_id and
flights_has_passengers.flights_id = flights.id and
flights.airplanes_id = a1.id
group by p1.id
having count(a1.id) = 1;
Hey i'm quite new to sql and i can't seem to find a good of a way to solve a problem.I have the following entities
passengers (`id` INT, `name` VARCHAR(45), `surname` VARCHAR(45),
`year_of_birth` INT)
flights (`id` INT, `routes_id` INT, `date` DATE, `airplanes_id` INT)
flights_has_passengers (`flights_id` INT, `passengers_id` INT)
airplanes (`id` INT, `number` VARCHAR(45), `manufacturer`
VARCHAR(45), `model` VARCHAR(45)).
I want to make a script that finds the names of all the passengers , that have boarded in atleast on flight and if boarded more than one,those flights must have been made using the same airplane.I tried to return a tuple containing the planes, that each one of the passengers had used,using a nested query.Then i tried to use an all statement, to check if every entry in the typle is the same.I got no luck though .Any advice would be appriciated.
EDIT : I thought the following solution but i don't know if it is correct:
select p1.name, p1.surname
from passengers p1, airplanes a1, flights_has_passengers, flights
where
p1.id = flights_has_passengers.passengers_id and
flights_has_passengers.flights_id = flights.id and
flights.airplanes_id = a1.id
group by p1.id
having count(a1.id) = 1;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:根据OP对答案的评论,这是另一种使用不存在的方式
也许使用下面的加入
UPDATE: Based on OP's comment on the answer, here's another way using NOT EXISTS
Maybe use a JOIN like below