일을 반복하지 않고 모두 한 번에 수행하십시오. 하루에 적어도 하나의 오류가 있다고 가정하면 errors
테이블에서이 정보를 얻을 수 있습니다. 그렇지 않으면 다음을 위해 캘린더 테이블이 필요할 수 있습니다.
SELECT dates.thedate, e.error_id, s.server_id, e.start_time, s.name
FROM (select distinct date(start_time) as thedate
from errors
where e.start_time BETWEEN '2014-02-25 00:00:00' AND '2014-03-03 23:59:59'
) dates cross join
servers s LEFT OUTER JOIN
errors e
ON e.server_id = s.server_id;
이렇게하면 하루에 각 서버의 각 오류에 대한 행이 생성됩니다. 오류가 없으면 각 서버에 대해 오류 필드에 NULL
행이 있습니다. 이 집계하려면 :
SELECT dates.thedate, s.server_id, s.name, count(*) as numErrors,
group_concat(error_id order by e.start_time) as errorIds,
group_concat(se.tart_time order by e.start_time) as startTimes
FROM (select distinct date(start_time) as thedate
from errors
where e.start_time BETWEEN '2014-02-25 00:00:00' AND '2014-03-03 23:59:59'
) dates cross join
servers s LEFT OUTER JOIN
errors e
ON e.server_id = s.server_id and date(e.start_time) = dates.thedate
GROUP BY dates.thedate, s.server_id, s.name;
편집 : 달력 테이블없이
, 당신은이 같은 쿼리로 매일을 삽입 할 수 있습니다
SELECT dates.thedate, s.server_id, s.name, count(*) as numErrors,
group_concat(error_id order by e.start_time) as errorIds,
group_concat(se.tart_time order by e.start_time) as startTimes
FROM (select date('2014-02-25') as thedate union all
select date('2014-02-26') union all
select date('2014-02-27') union all
select date('2014-02-28') union all
select date('2014-03-01') union all
select date('2014-03-02') union all
select date('2014-03-03')
) dates cross join
servers s LEFT OUTER JOIN
errors e
ON e.server_id = s.server_id and date(e.start_time) = dates.thedate
GROUP BY dates.thedate, s.server_id, s.name;
다른 쿼리에서이 작업을 수행 할 수있는 경우 그러면 오류가있는 모든 서버 ID를 가져 와서 서버 테이블에서 선택 쿼리를 수행 할 수 있습니다. "server_id NOT IN (서버 _ 오류 _ ID)"여기서 모든 서버 ID를 얻을 수 있습니다. – Tommy
모두 선택 가능 그런 다음 서버를 빼고 빼기 연산자를 사용하십시오. 모든 나쁜 것들? – Naruto
@ 토미 그 일을 생각했습니다. 가지고있는 쿼리와 일치하는 결과를 ID의 배열에 추가하십시오. 이 배열을 두 번째 쿼리의 WHERE 클로 우로 피드 하시겠습니까? 나는 어제 그것을 실제로 시험해 보았지만! = 작동하지 않는 몇 가지 문제에 부딪쳤다. 어디에서 claus에 배열을 공급할 수있는 특별한 방법이 있습니까? – Envious