sql - 如何根据书名计算已售出的图书总数?
问题描述
select b.ISBN, b.title, a.author_name, b.unit_price, p.quantity
from books as b left join
purchases as p
on b.ISBN = p.book_id left join
authors as a
on b.author_id = a.id;
解决方案
您可以使用分组方式:
select
b.ISBN,
b.title,
a.author_name,
b.unit_price,
sum(p.quantity) as Sold
from
books as b
left join purchases as p on b.ISBN = p.book_id
left join authors as a on b.author_id = a.id
group by
b.ISBN,
b.title,
a.author_name,
b.unit_price
推荐阅读
- mysql - 仅使用部分二进制日志 MySql 恢复数据库
- computer-vision - 质心算法
- javascript - 简单的 AJAX 似乎悄悄地压倒了浏览器
- android - Flutter:在滚动时将简单对话框转换为全屏对话框
- python - 计算 Pandas Dataframe 中特定列中的列表条目
- azure-active-directory - Group.ReadWrite.All 范围说明
- ajax - 在 Laravel 控制器中请求输入 null
- javascript - Rails 6 + Webpacker:未捕获的 TypeError:$(...).select2 不是函数
- progressive-web-apps - TWA 基础应用程序有时显示 URL,有时工作,甚至 Assetlink.json 正确上传
- python - 如何使用 Postgres 窗口函数或横向连接限制 Django ORM 中每个组的前 N 个?