我向我的表发出以下查询:
SELECT code_ver,
platform_type,
category_name
FROM builds
WHERE runtype LIKE 'test'
AND platform_type NOT LIKE 'total';
+-----------+---------------+---------------+
| code_ver | platform_type | category_name |
+-----------+---------------+---------------+
| 10.0.1.50 | 8k | UMTS |
| 10.0.1.50 | 8k | UMTS |
| 10.0.1.50 | 8k | IP |
| 10.0.1.50 | 8k | IP |
| 10.0.1.50 | 9k | IP |
| 10.0.1.50 | 9k | IP |
| 10.0.1.50 | 9k | UMTS |
| 10.0.1.50 | 9k | UMTS |
| 10.0.1.50 | 9k | UMTS |
| 10.0.1.50 | 9k | Stability |
| 10.0.1.50 | 9k | Stability |
| 10.0.1.51 | 8k | UMTS |
| 10.0.1.51 | 8k | UMTS |
| 10.0.1.51 | 8k | IP |
| 10.0.1.51 | 8k | IP |
| 11.0.1.50 | 9k | UMTS |
| 11.0.1.50 | 9k | IP |
+-----------+---------------+---------------+
我触发以下查询
SELECT code_ver,
platform_type,
Count(*)
FROM builds
WHERE runtype LIKE 'test'
AND platform_type NOT LIKE 'total'
GROUP BY code_ver,
platform_type;
+-----------+---------------+----------+
| code_ver | platform_type | count(*) |
+-----------+---------------+----------+
| 10.0.1.50 | 8k | 4 |
| 10.0.1.50 | 9k | 7 |
| 10.0.1.51 | 8k | 4 |
| 11.0.1.50 | 9k | 2 |
+-----------+---------------+----------+
当我触发以下查询时
SELECT code_ver,
platform_type,
Count(*)
FROM builds
WHERE runtype LIKE 'test'
AND platform_type NOT LIKE 'total'
GROUP BY code_ver,
platform_type,
category_name;
+-----------+---------------+----------+
| code_ver | platform_type | count(*) |
+-----------+---------------+----------+
| 10.0.1.50 | 8k | 2 |
| 10.0.1.50 | 8k | 2 |
| 10.0.1.50 | 9k | 2 |
| 10.0.1.50 | 9k | 2 |
| 10.0.1.50 | 9k | 3 |
| 10.0.1.51 | 8k | 2 |
| 10.0.1.51 | 8k | 2 |
| 11.0.1.50 | 9k | 1 |
| 11.0.1.50 | 9k | 1 |
+-----------+---------------+----------+
但我想要 (code_ver + platform_type + category_name) 的唯一组合的计数,这样输出应该如下所示:
+-----------+---------------+----------+
| code_ver | platform_type | count(*) |
+-----------+---------------+----------+
| 10.0.1.50 | 8k | 2 |
| 10.0.1.50 | 9k | 3 |
| 10.0.1.51 | 8k | 2 |
| 11.0.1.50 | 9k | 2 |
+-----------+---------------+----------+
谁能给我一些建议
最佳答案
使用 SQL 内部查询。
SELECT ta.code_ver,
ta.platform_type,
Count(*)
FROM (SELECT code_ver, platform_type
FROM builds
WHERE runtype LIKE 'test'
AND platform_type NOT LIKE 'total'
GROUP BY code_ver, platform_type, category_name) AS ta -- table alias
GROUP BY ta.code_ver, ta.platform_type;
关于MySQL 查询 - 分组值和聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16227834/