开发者

How to find the difference between 2 times in MySQL-like db in a single query

I have a table with the following schema:

+------------------+
|Field  | Type     |
+------------------+
| id    | char(30) |
| time  | datetime |
+------------------+

The id column is NOT a primary key, so therefore, the table contains entries such as:

10, 2010-05-05 20:01:01
10, 2010-05-05 20:01:05
13, 2010-05-05 20:04:01
10, 2010-05-05 20:04:10
13, 2010-05-05 20:05:03
10, 2010-05-05 20:05:28

I want to find the difference between times for a given id, i.e. the desired results would be:

10, 00:04:27
13, 00:01:02

I can do this using the following query in MySQL:

SELECT t2.id, TIMEDIFF(t2.max, t2.min) AS visit_length FROM
(
  SELECT id, MAX(time) AS max, MIN(time) AS min FROM page_view 
  GROUP BY id
) AS t2

However, the engine I'm using is a MySQL derivative that doesn't support su开发者_如何学编程bqueries.

Is there any way of achieving the same results with a single query without using subqueries?

Thanks


Will this work?

SELECT TIMEDIFF(MAX(time), MIN(TIME)) AS visit_length
FROM page_view
GROUP BY(id)


select max(time)-min(time), id
from page_view
group by id
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜