问题描述
i有一个包含以下字段的表:
id,name,dept,pay
1,John,Sales ,4000
2,Peter,HR,5000
等
我如何计算每个人中有多少人有一个SQL查询的部门吗?
我发现这个令人惊讶的困难,因为我可以通过使用以下方式总结每个部门的总额
的工资: />
$ query ="
选择总和(if(dept =''HR'',pay,0))为hrpay,......
" ;;
TIA
Hi,
i have a table that has the following fields:
id, name, dept, pay
1, John, Sales, 4000
2, Peter, HR, 5000
etc.
How do i count how many people there are in each dept with an sql query?
I find this surprisingly difficult, given the fact that i can sum the total
pay for each dept by using:
$query="
Select sum(if (dept=''HR'', pay, 0)) as hrpay, ......
";
TIA
推荐答案
SELECT Count(dept)AS deptcount
FROM [table]
GROUP BY id
SELECT Count(dept) AS deptcount
FROM [table]
GROUP BY id
这篇关于Mysql计数(条件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!