Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 需要查询两个字段的不同组合,以及出现不同组合的计数_Mysql_Sql_Oracle_Count_Combinations - Fatal编程技术网

Mysql 需要查询两个字段的不同组合,以及出现不同组合的计数

Mysql 需要查询两个字段的不同组合,以及出现不同组合的计数,mysql,sql,oracle,count,combinations,Mysql,Sql,Oracle,Count,Combinations,我需要的是一个表上的查询,该查询将返回a列和B列的不同组合,以及每个组合在表中出现的次数计数。这将全部按A列排序 如果表格为: A B ....... 1 1 1 1 1 1 1 2 2 1 2 1 结果将是: A B count 1 1 3 1 2 1 2 1 2 任何帮助都会很好。小组成员是您的朋友: select a,b,count(*) from test group by

我需要的是一个表上的查询,该查询将返回a列和B列的不同组合,以及每个组合在表中出现的次数计数。这将全部按A列排序

如果表格为:

A    B  .......
1    1
1    1
1    1
1    2
2    1
2    1
结果将是:

A    B     count
1    1       3
1    2       1
2    1       2

任何帮助都会很好。

小组成员是您的朋友:

select a,b,count(*) from test
group by a,b
order by a

SQLFiddle:

像这样使用
groupby

SELECT
    `A`,
    `B`,
    COUNT(*) AS `Count`
FROM
    `table`
GROUP BY
    `A`, `B`
ORDER BY
    `A`

谷歌搜索并了解
分组方式如何工作我注意到您的表没有主键,这可能会进一步证明有问题