mysql分配给任务的人数

时间:2009-11-24 12:21:13

标签: mysql database

我有以下表格......

任务 alt text http://img260.imageshack.us/img260/3695/screenshotbg.png

alt text http://img685.imageshack.us/img685/6445/screenshot1f.png

tasks_people alt text http://img260.imageshack.us/img260/1061/screenshot2r.png

...我希望获得一份任务列表,并分配给他们的人数。请注意,可以多次将人员分配到任务(在不同日期)。出于某种原因,我似乎无法管理它。

感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

如果你想让同一个人的许多作业被算作1做:

select tasks.task_id, tasks.title, count(distinct tasks_people.people_id) 
as p_counter
from tasks left join tasks_people
on tasks.task_id = tasks_people.task_id
group by tasks.task_id

否则只需count()

select tasks.task_id, tasks.title, count(tasks_people.people_id) as p_counter
from tasks left join tasks_people
on tasks.task_id = tasks_people.task_id
group by tasks.task_id

答案 1 :(得分:0)

我无法看到您的表格结构,但您可以尝试从tasks_people(distinct taskid,personid)中选择不同的值,然后进行计数和分组。

答案 2 :(得分:0)

select task_id, count(distinct people_id) as people_assigned_to_task
from tasks_people
group by task_id

如果计数+明显不正常(即花费太长时间),你可以试试这个:

select task_id, 
(select count(*) from 
      (
          select distinct people_id from tasks_people i 
          where i.task_id = o.task_id
      ) 
) as people_assigned_to_task
from tasks_people o
group by task_id