我正在尝试添加 MySQL GROUP BY 的结果,但我无法做到这一点,这是我的代码:
SELECT country,SUM(visits) AS visits,SUM(visits-1) AS repetidos, COUNT(1) AS total FROM stats GROUP BY country HAVING COUNT(1) > 1
附上我的代码结果的图片以及我希望通过以下方式实现的目标:
我还会捕获我的表格:
最佳答案
也许您需要添加汇总
drop table if exists t;
create table t
(country varchar(2),visits int);
insert into t values
('co',12),('co',20),
('us',15),('us',1),('us',1);
SELECT country,SUM(visits) AS visits,
SUM(visits-1) AS repetidos,
COUNT(1) AS total
FROM t
GROUP BY country with rollup
HAVING COUNT(1) > 1;
+---------+--------+-----------+-------+
| country | visits | repetidos | total |
+---------+--------+-----------+-------+
| co | 32 | 30 | 2 |
| us | 17 | 14 | 3 |
| NULL | 49 | 44 | 5 |
+---------+--------+-----------+-------+
3 rows in set (0.00 sec)
关于mysql - 如何对 GROUP By 的结果求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58577642/