sql - 如何获取客户数量?
问题描述
我需要一些有关 SQL 查询的帮助。
假设我们有一个管理酒店预订的示例数据库:
Customer(ID, name, birthDate, city);
Room(number, floor, beds, roomtype, price);
Bookings(Customer, roomNumber, floor, checkInDate, checkOutDate);
我需要知道哪些客户只预订了经济型客房。这是我的查询:
select Customer from Bookings
join Room on(Bookings.num = camera.roomNumber and Bookings.floor=
Room.floor)
where (Bookings.Customer, Bookings.floor) not in (select number, floor from
Room where roomType != 'economic')
我的问题是这个查询向我显示了预订经济房的客户,但它也向我显示了预订其他类型房间的客户。如何限制输出以获取仅预订经济房的客户?谢谢
解决方案
您可以使用聚合:
select b.Customer
from Bookings b join
Room r
on b.num = r.roomNumber and b.floor = r.floor
group by b.Customer
having min(roomType) = max(roomType) and min(roomType) = 'economic';
推荐阅读
- mongodb - MongoDB 使用 group 和 unwind 计算出现次数
- java - 错误:不兼容的类型:
无法使用 android room 转换为 int if (!_cursor.isNull(null)) - java - 华为 HIAI 引擎“通用文本识别”未检测到图像中的任何文本
- c# - 如何使用不同的提供商从 Acumatica ERP 发送 SMS?
- docker - 当我从 Docker 容器连接到远程数据库时,远程看到的 IP 地址是什么?
- gnu-make - 如果未定义变量,则 nmake 抛出错误
- javascript - 带有提交按钮的链接的下拉图像更改
- semantic-ui - 语义 UI 细分浮动属性的使用
- sql - SQL - 在连接相关记录的同时更新另一个表中的列?
- android - libapp.so 和 libflutter.so 是什么?