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;
查询结果