小编Yam*_*ada的帖子

MySQL SUM 在同一个表上有多个 GROUP BY

根据 MySQL 中同一个表上的不同 group by 条件生成多个求和结果的最佳方法是什么?

我相信我的选择很糟糕,但我想不出更好的方法。

想象一下有一个订单表,我在其中跟踪下订单的客户、它的价值和订单的一些限定符(类型)。我想对按订单类型组合分组的给定客户的订单总数求和:

delimiter $$

CREATE TABLE `orders` (
  `idorder` int(11) NOT NULL,
  `value` double DEFAULT NULL,
  `idclient` int(11) DEFAULT NULL,
  `type1` int(11) DEFAULT NULL,
  `type2` int(11) DEFAULT NULL,
  `type3` bit(1) DEFAULT NULL,
  PRIMARY KEY (`idorder`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$
Run Code Online (Sandbox Code Playgroud)

我的选择:

SELECT T0.idclient, T1.condition1, T2.condition2, T3.condition3 FROM
(SELECT 1 AS idclient) AS T0
LEFT JOIN
(SELECT idclient, SUM(value) condition1 FROM `test`.`orders` WHERE idclient = 1 AND type1 = 1 AND type2 = 1 AND type3 …
Run Code Online (Sandbox Code Playgroud)

mysql performance query-performance

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

mysql ×1

performance ×1

query-performance ×1