[英]sql select all record for all
so there is 3 tables 所以有3张桌子
1 passanger detail 1个passanger详细信息
2 booking 2预订
3 flight 3次飞行
passanger 过客
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
+----+------+
booking 订票
+----+-------+
| id |flight |
+----+-------+
| 1 | 101 |
| 1 | 102 |
| 1 | 103 |
| 2 | 101 |
| 3 | 104 |
| 2 | 105 |
+----+-------+
flight 飞行
+--------+------+
| flight | late |
+--------+------+
| 101 | 80 |
| 102 | 80 |
| 103 | 80 |
| 104 | 10 |
| 105 | 10 |
+--------+------+
table 1 contains passnger id and name 表1包含密码ID和名称
table 2 contains passanger id and its flight id 表2包含Passanger ID及其航班ID
table 3 contains flight id and how much minute it is late 表3包含航班号及其延迟的分钟数
now i want to find out name of the passanger whose all flights are 50 minutes late or more 现在我想找出所有航班晚点50分钟或更长时间的乘客的名字
so the output will be A
because 101 102 and 103 are late by 80 min 因此输出将为
A
因为101102和103延迟了80分钟
not B
because 101 is 80 mins late but 105 is 10 mins late (not all of b's flights are late by 50) 不是
B
因为101晚了80分钟,但105晚了10分钟(并非b的所有航班都晚了50分钟)
so my approach is 所以我的方法是
select name from passanger,booking where passanger.id=booking.id and booking.flight = ALL (select flight.flight from flight where flight.late>50)
can't get the desired output. 无法获得所需的输出。
You can group by name and check if all the flights of a passenger are late with a having
condition. 您可以通过名字可以组和检查,如果乘客的所有航班下旬一个
having
状态。
select p.name
from passanger p
join booking b on p.id=b.id
join flight f on f.flight = b.flight
group by p.name
having count(*) = count(case when f.late>50 then f.flight end)
Try this one: 试试这个:
select name from passanger, booking
where passanger.id = booking.id
and booking.flight in (select flight.flight from flight
where flight.late > 50)
or 要么
select name from passanger, booking, flight
where passanger.id = booking.id and booking.flight = flight.flight and flight.late > 50
This will result in a's name 3 times and b's name once but if you want to get only one instance of a and b in the result, you can use select distinct name
. 这将导致a的名称出现3次,b的名称出现一次,但是如果您只想在结果中获得a和b的一个实例,则可以使用
select distinct name
。
select ps.name from passanger ps left join booking bk on (ps.id = bk.id) left join flight fl on (fl.flight = bk.flight) where late >50
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.