Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.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 - Fatal编程技术网

统计表中的出现次数(MySQL)

统计表中的出现次数(MySQL),mysql,sql,Mysql,Sql,总之,我有一个表,有两列,可以有任意数量的行,类型和位置名称可以是任意的,尽管类型总是按字母顺序排列- Type Location Inmate Holding Inmate Holding Inmate Processing Person Processing Person Reception Person Reception Person Reception Visitor Processing Visitor Reception Visitor Reception

总之,我有一个表,有两列,可以有任意数量的行,类型和位置名称可以是任意的,尽管类型总是按字母顺序排列-

Type    Location

Inmate  Holding
Inmate  Holding
Inmate  Processing
Person  Processing
Person  Reception
Person  Reception
Person  Reception
Visitor Processing
Visitor Reception
Visitor Reception
我需要计算类型相对于位置的出现次数,因此结果将是-

Quantity    Type    Location

2   Inmate  Holding
1   Inmate  Processing
1   Person  Processing
3   Person  Reception
1   Visitor Processing
2   Visitor Reception
我曾想过使用selecttype(Count*etc),但这只是添加了所有类型,并告诉我有10行,selectdistinct类型(Count)似乎不起作用

我相信我可以用变量来实现这一点,检测类型或位置名称的更改等,但我更喜欢不必创建存储过程(在MySQL中)

有别的方法吗

问候,活动

尝试:

select Type, Location , count(*) from table group by Type, Location;

您是否已尝试按两列分组,并在中选择将计数排在其他列之前

SELECT COUNT(*), Type, Location from TABLE GROUP BY Type, Location

关系数据库中的“类型总是按字母顺序排列”数据是集合,因此它们是无序的。感谢Umais和Krishnakumar(工作正常)。幸运的是,这个论坛包括了比我思维更具逻辑性的人。很高兴能提供帮助。如果对你有好处,你可以接受这个解决方案作为答案。遗憾的是,你如何接受这个解决方案作为答案如果对你有好处,我并不清楚,似乎没有按钮/链接可以这样做,所以我接受你在这篇评论中的答案。啊,找到了(大勾号)。