MYSQL - 按分组计算

问题描述 投票:-1回答:2

假设这样的表

domain       | city      | number
-------------|-----------|-------
example.com  | rome      | 11111 <--,
example1.com | rome      | 11111    |_ this two should be counted as 1
example2.com | rome      | 11111    |
example.com  | rome      | 11111 <--'
example.com  | amsterdam | 11111
example2.com | rome      | 22222
example.com  | uk        | 22222
example.com  | amsterdam | 22222 <--._ this two should be counted as 1
example.com  | amsterdam | 22222 <--'

我想输出类似以下结果11111, total = 422222, total = 3

到目前为止,我有这个查询,但当然不能正常工作! :(

SELECT number, COUNT(number) as total FROM table GROUP BY city, domain
mysql group-by count
2个回答
1
投票

您可以在预处理表表达式上使用COUNT()来消除重复项,如下所示:

select number, count(*)
from (
  select distinct domain, city, number from my_table
) x
group by number

1
投票

你可以group by number

select number, count(distinct concat(domain, '-', city)) total
from tablename
group by number

结果:

number  total
11111   4
22222   3
© www.soinside.com 2019 - 2024. All rights reserved.