mysql 查询每个订单总价和购买商品的总类数

数据表
CREATE TABLE goods (
order_id INT NOT NULL,
goods_id INT NOT NULL,
price DECIMAL(10, 2) NOT NULL
);
插入数据
INSERT INTO goods (order_id, goods_id, price) VALUES
(1, 1, 3.5),
(1, 2, 4.0),
(2, 4, 6.0);

数据查询
SELECT
order_id,
SUM(price) AS total_price,
COUNT(goods_id) AS total_goods
FROM
goods
GROUP BY
order_id;
查询结果

posted @ 2024-11-17 15:21  北京小小鸟  阅读(11)  评论(0编辑  收藏  举报