sql:join和count

时间:2014-09-16 06:18:01

标签: mysql sql join

我的MySQL数据库中有三个表。

table department (int id, int departent_id);
table position (int id, int department_id)
table test (int id, int position_id)
部门有多个职位,考试有多个部门。 我需要计算每个部门的测试数量。

2 个答案:

答案 0 :(得分:1)

SELECT count(*), d.department_id FROM department d 
INNER JOIN position p ON p.department_id = d.department_id
INNER JOIN test t ON t.position_id = p.id
GROUP BY d.department_id

group语句按department_id

对计数进行分组

答案 1 :(得分:0)

SELECT COUNT(*), department.id 
FROM test 
INNER JOIN department 
ON department.id = test.department_id 
GROUP BY department.id;

这样的东西?

与您提出的问题相关的职位是什么?