gpt4 book ai didi

MySQL 多表子查询

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

我正在尝试使用子查询检索预订伦敦酒店的所有客人的姓名和地址(按姓名字母顺序在 MySQL 中排序)并收到此错误:

Error Code: 1242. Subquery returns more than 1 row

这是我运行的查询:

select * from guest 
where guest_no =
(
select guest_no
from booking
where hotel_no = (select hotel_no
from hotel
where city = 'London')
);

这是酒店、预订和客人的架构:

hotel (hotel_no, hotel_name, city)

booking (hotel_no, guest_no, date_from, date_to, room_no)

guest (guest_no, g_name, g_address)

此外,这是房间的架构:

room (room_no, hotel_no, type, price)

请帮助我解决上述错误和可能的解决方案。

感谢和问候。

最佳答案

为什么不使用 join as

select
g.guest_no,
g.g_name,
g.g_address
from guest g
inner join booking b on b.guest_no = g.guest_no
inner join hotel h on h.hotel_no = b.hotel_no
where h.city = 'London'

关于MySQL 多表子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23422553/

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