2009-11-24 3 views
0

나는 다음과 같은 테이블 alt text http://img260.imageshack.us/img260/1061/screenshot2r.pngMySQL의 번호

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

tasks_people ...

작업 alt text http://img260.imageshack.us/img260/3695/screenshotbg.png

사람이 ... 내가 원하는 사람 수를 지정하여 작업 목록을 가져옵니다. 한 사람을 다른 날짜에 두 번 이상 작업에 배정 할 수 있습니다. 웬일인지 나는 그것을 관리 할 수없는 것처럼 보인다.

도움 주셔서 감사합니다.

+1

"한 사람이 한 번 이상 작업에 배정 될 수 있음"이라는 귀하의 메모와 관련하여 이들 모두를 1 또는 다수로 계산합니까? –

답변

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 
0

테이블 구조를 볼 수 없지만 tasks_people (distinctidid, personid)에서 고유 값을 선택한 다음 개수 및 그룹화를 시도 할 수 있습니다.

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