gpt4 book ai didi

mysql - sql 选择所有记录

转载 作者:行者123 更新时间:2023-11-29 11:09:02 25 4
gpt4 key购买 nike

所以有3张 table

1 名乘客详细信息

2次预订

3个航类

乘客

+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
+----+------+

预订

+----+-------+
| id |flight |
+----+-------+
| 1 | 101 |
| 1 | 102 |
| 1 | 103 |
| 2 | 101 |
| 3 | 104 |
| 2 | 105 |
+----+-------+

航类

+--------+------+
| flight | late |
+--------+------+
| 101 | 80 |
| 102 | 80 |
| 103 | 80 |
| 104 | 10 |
| 105 | 10 |
+--------+------+

表 1 包含乘客 ID 和姓名

表2包含乘客ID及其航类ID

表 3 包含航类 ID 以及晚点的分钟

现在我想找出所有航类晚点 50 分钟或以上的乘客姓名

因此输出将为 A 因为 101 102 和 103 迟到了 80 分钟

不是 B,因为 101 晚点 80 分钟,但 105 晚点 10 分钟(并非所有 b 的航类都晚点 50 分钟)

所以我的方法是

select name from passanger,booking where passanger.id=booking.id and booking.flight = ALL (select flight.flight from flight where flight.late>50)

无法获得所需的输出。

最佳答案

您可以按姓名分组,并检查某个乘客的所有航类是否都晚点且符合 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)

关于mysql - sql 选择所有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40940356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com