使用SQL语句创建视图room_order_summary 用于查看每个房间类型的预订数量和总费用 表名:customer——info room_infoorde——info
时间: 2023-11-02 19:05:03 浏览: 153
好的,以下是创建视图room_order_summary的SQL语句:
```
CREATE VIEW room_order_summary AS
SELECT r.room_type, COUNT(o.order_id) AS order_count, SUM(o.price) AS total_cost
FROM room_info r
LEFT JOIN order_info o ON r.room_id = o.room_id
GROUP BY r.room_type;
```
解释一下这个语句的各个部分:
- `CREATE VIEW room_order_summary`:创建一个名为`room_order_summary`的视图。
- `SELECT r.room_type, COUNT(o.order_id) AS order_count, SUM(o.price) AS total_cost`:从`room_info`和`order_info`表中选择房间类型、预订数量和总费用,并分别给它们取别名`order_count`和`total_cost`。
- `FROM room_info r LEFT JOIN order_info o ON r.room_id = o.room_id`:从`room_info`表和`order_info`表中获取数据,并按照`room_id`字段进行左连接(LEFT JOIN)。
- `GROUP BY r.room_type`:按照房间类型进行分组。
这样,你就可以使用以下语句查看每个房间类型的预订数量和总费用:
```
SELECT * FROM room_order_summary;
```
阅读全文