CREATE VIEW flower_sales ASSELECT flower.name, SUM(sale.quantity) AS total_quantity, SUM(sale.quantity * flower.price) AS total_salesFROM flowerINNER JOIN sale ON flower.id = sale.flower_idGROUP BY flower.id;
时间: 2024-03-10 11:48:42 浏览: 51
flowersale.sql
这是一个创建名为 `flower_sales` 的视图的 SQL 语句,该视图将花卉表和销售表联接,并计算每种花卉的总销售数量和总销售额。
你可以使用以下 SQL 语句创建该视图:
```sql
CREATE VIEW flower_sales AS
SELECT flower.name, SUM(sale.quantity) AS total_quantity, SUM(sale.quantity * flower.price) AS total_sales
FROM flower
INNER JOIN sale ON flower.id = sale.flower_id
GROUP BY flower.id;
```
请注意,这里使用了 `INNER JOIN` 来联接两个表,并在 `SELECT` 语句中使用了聚合函数 `SUM()` 来计算总销售数量和总销售额。最后通过 `GROUP BY` 对每种花卉进行分组,以便正确计算总销售数量和总销售额。
阅读全文