从三月份的订单中获取名称,日期和总和

时间:2019-03-27 19:28:42

标签: mysql sql

我有这个查询:

SELECT * 
FROM ((orders_books 
JOIN books ON orders_books.id_book = books.id) 
JOIN orders ON orders_books.id_order=orders.id) 
JOIN users ON user_id=users.id 
WHERE date >='2019-03-1' AND date <= '2019-03-31';

查询结果:https://image.prntscr.com/image/s3ngzzYrSd27mFib9HbLmA.png

我找到订单数:

SELECT name, date, SUM(book_price) 
FROM ((orders_books 
INNER JOIN books ON orders_books.id_book = books.id) 
INNER JOIN orders ON orders_books.id_order=orders.id) 
INNER JOIN users ON user_id=users.id 
WHERE date >='2019-03-1' AND date <= '2019-03-31';

[所有订单金额]:https://image.prntscr.com/image/o88Hf4uKSHGuZ2ESUECzuA.png

但是我需要分别找到每个订单的订单金额。

3 个答案:

答案 0 :(得分:0)

怎么样:

SELECT id_order, SUM(book_price) 
FROM ((orders_books 
INNER JOIN books ON orders_books.id_book = books.id) 
INNER JOIN orders ON orders_books.id_order=orders.id) 
INNER JOIN users ON user_id=users.id 
WHERE date >='2019-03-1' AND date <= '2019-03-31'
GROUP BY id_order;

答案 1 :(得分:0)

SELECT orders.id, SUM(book_price) 
FROM ((orders_books 
INNER JOIN books ON orders_books.id_book = books.id) 
INNER JOIN orders ON orders_books.id_order=orders.id) 
INNER JOIN users ON user_id=users.id 
WHERE date >='2019-03-1' AND date <= '2019-03-31'
GROUP BY order_id;

答案 2 :(得分:0)

如果您需要order.id的总和,请使用order.id分组

    SELECT orders.id  SUM(book_price) 
    FROM orders_books 
    INNER JOIN books ON orders_books.id_book = books.id
    INNER JOIN orders ON orders_books.id_order=orders.id

    WHERE date >='2019-03-1' AND date <= '2019-03-31'
    group by orders.id