2

以下のデータを含むMySQLテーブル「ログ」があります

id  username    status   statusdatetime   mailid
-----------------------------------------------------------------------
1   abc         pull     3/21/2013 10:04  1753
2   abc         step1    3/21/2013 10:13  1753
3   abc         step2    3/21/2013 10:17  1753
4   abc         step1    3/21/2013 10:46  1753
5   abc         step2    3/21/2013 10:54  1753
6   abc         step3    3/21/2013 11:09  1753
7   abc         mailsent 3/21/2013 11:10  1753
8   abc         pull     3/21/2013 11:11  2113
9   abc         step1    3/21/2013 11:18  2113
10  abc         step1    3/21/2013 11:32  2113
11  abc         step2    3/21/2013 11:33  2113
12  abc         step3    3/21/2013 11:44  2113
13  abc         mailsent 3/21/2013 11:44  2113
14  def         pull     3/21/2013 10:21  120
15  def         step1    3/21/2013 10:22  120
16  def         step2    3/21/2013 10:36  120
17  def         step1    3/21/2013 10:37  120
18  def         step2    3/21/2013 10:38  120
19  def         step3    3/21/2013 10:39  120
20  def         mailsent 3/21/2013 10:39  120
21  def         pull     3/21/2013 10:40  1203
22  def         step1    3/21/2013 10:41  1203
23  def         step2    3/21/2013 10:50  1203
24  def         step3    3/21/2013 10:54  1203
25  def         mailsent 3/21/2013 10:55  1203

私が要求するヘルプは以下の出力です:

id  username    mailid  Time (Seconds)
-----------------------------------------------------------------------
1   abc         1753    3977
2   abc         2113    1991
3   def         120     1101
4   def         1203    888

説明:

各ユーザーと各メール ID がグループ化され、「ステータス」列から「mailsent」と「pull」の時間差が計算されます。

これは1つの選択クエリで可能ですか?

4

2 に答える 2

1

このクエリを試してください

SELECT  
   a.username,
   a.mailid, 
   TIME_TO_SEC(TIMEDIFF(b.statusdatetime, a.statusdatetime)) AS DIFF
FROM 
   tbl a 
INNER JOIN
   tbl b 
ON 
   a.mailid = b.mailid AND 
   a.status = 'pull' AND
   b.status = 'mailsent'
GROUP BY 
   username, 
   mailid

フィドル

于 2013-04-02T07:26:00.367 に答える