2

Hello I need help with a query on a MySQL database.

I have a table that looks like this:

ID  TRACKID         DATE                    Name    Action
38  2013-08-12-36   2013-08-12 14:54:50 John Smith  0
37  2013-08-12-39   2013-08-12 14:54:28 John Smith  3
36  2013-08-12-39   2013-08-12 14:53:24 John Smith  4
35  2013-06-12-91   2013-08-12 14:30:01 John Smith  3
34  2013-06-12-91   2013-08-12 14:29:44 John Smith  4
31  2013-06-12-91   2013-08-12 14:28:39 John Smith  0

I want to list the time difference between ACTION=0 and ACTION=3 for each unique TRACKID between a specified date.

SELECT DISTINCT TIMEDIFF
       (
                (select `date` from hesk_history where `action` = 0),
                (select `date` from hesk_history where `action` = 3 )
       ) AS DIFF

FROM    hesk_history

WHERE `date` BETWEEN '2013-08-12 00:00:00' AND '2013-08-12 23:59:59'

The solution should list each distinct TRACKID and the TIMEDIFF between Action 0 And Action 3 when these action exist for each TRACKID.

TRACKID       DIFF
2013-06-12-91 -00:01:22
4

2 回答 2

0

尝试像这样加入表本身:

SELECT 
    t1.TRACKID,
    t2.date - t1.date
FROM
    table t1
    JOIN table t2 ON t2.TRACKID = t1.TRACKID AND t1.Action = 0 AND t2.Action = 3
WHERE t1.date BETWEEN '2013-08-12 00:00:00' AND '2013-08-12 23:59:59'
于 2013-08-12T22:21:59.470 回答
-1

下面应该可以工作,我不确定 DATEDIFF 函数是否在 MySQL 上,但如果不太确定它有自己的。


SELECT A.TrackID,
       DATEDIFF(A.TrackID,B.TrackID)
FROM hesk_history A
INNER JOIN hesk_history B
ON A.TrackID = B.TrackID AND B.Action=3
WHERE A.TrackID == <your_input> AND A.Action=0
于 2013-08-12T22:28:18.830 回答