首页 > 解决方案 > 如何根据书名计算已售出的图书总数?

问题描述

截屏

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;

标签: sqlsqlite

解决方案


您可以使用分组方式:

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


推荐阅读