2013-01-23 3 views
1

MySQL에서 관련된 일별로 그룹화 된 두 개의 타임 스탬프 TIMEDIFF를 얻는 방법을 찾고 있습니다.TIMEDIFF GROUP BY 관련 일

SELECT TIMEDIFF('2012-01-02 10:00:00', '2012-01-01 10:00:00'); 

이것은 단지 24 시간의 차이를 제공합니다.

나는 둘 다 (또는 아마 더 많은) 일의 diff가 필요합니다. 예 :

2012-01-01: 14:00:00 
2012-01-02: 10:00:00 
[...] 

TIMEDIFF 기능으로 그룹화 할 수있는 방법이 있습니까?

+0

명확히하기 : 당신은 날짜 시간의 목록을 가서 각각의 DIFF를 얻으려면 하나 (즉 1과 2, 2와 3 사이)? – pzirkind

+0

아니요, 두 개의 타임 스탬프 timestamp1 (2012-01-01 ...) 및 timestamp2 (2012-01-09 ...)가 있습니다. 결과는 1 월 1 일부터 9 일까지의 날짜 목록과 비례 시간/분/초이어야합니다. – Thomas1703

+2

아, 질문에 원하는 결과를 게시하여보다 잘 도와 드리겠습니다. – pzirkind

답변

1

원하는 작업을 수행하려면 일련의 숫자를 생성해야합니다. 아마도 당신은 테이블을 가지고있을 것입니다. 그렇지 않을 경우, 다음과 같은 :

select dstart + interval n.num days 
from (select d1*10+d2 as num 
     from (select 0 as d union all select 1 union all select 2 union all 
      select 3 union all select 4 union all select 5 union all select 6 union all 
      select 7 union all select 8 union all select 9 
      ) d1 cross join 
      (select 0 as d union all select 1 union all select 2 union all 
      select 3 union all select 4 union all select 5 union all select 6 union all 
      select 7 union all select 8 union all select 9 
      ) d2 
    ) n join 
     (select date('2012-01-02') as dend, date('2012-01-01') dstart 
     on dstart + interval n.num days <= dend 

(이 구문 오류가있을 수 있습니다, 그래서이 테스트되지 않습니다.)