统计每个部门的用户

时间:2015-06-03 08:36:35

标签: mysql

我有一张包含以下布局的表格:


+----+-----------+-------------+
| id | name      | department  |
+----+-----------+-------------+
|   1| John      | Finance     |
+----+-----------+-------------+
|   2| Bob       | Optics      |
+----+-----------+-------------+
|   3| Jill      | Finance     |
+----+-----------+-------------+
|   4| Jake      | Finance     |
+----+-----------+-------------+
|   5| Mike      | Support     |
+----+-----------+-------------+

我想得到每个部门所有人的总和:

+-----------+-------------+
| people    | department  |
+-----------+-------------+
|          3| Finance     |
+-----------+-------------+
|          1| Optics      |
+-----------+-------------+
|          1| Support     |
+-----------+-------------+

我在构建正确的查询时遇到了问题

3 个答案:

答案 0 :(得分:2)

使用SELECT COUNT(name),department FROM table GROUP BY department

RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=([^&]+) [NC] 
RewriteRule ^example.html$ https://www.url.com/example/new-page.html? [NC,L,R]

答案 1 :(得分:1)

SELECT departmetn, Count(*) as people FROM table Group by departmetn

答案 2 :(得分:1)

试试这个: -

 SELECT COUNT(name) AS people,department FROM table GROUP BY department