我的表就像这样,当用户ID为A或B时,我想得到Maxtime和Mintime。
user_id time
---------- ----------
A 2016-01-03 23:02:35
A 2016-01-03 23:02:36
A 2016-01-03 23:02:38
B 2016-01-03 22:02:35
B 2016-01-03 22:02:39
我的代码是:
SELECT user_id,Max(time)AS [start], Min(time) AS [end]
FROM client1
WHERE user_id is not null
GROUP BY user_id
HAVING user_id in (select user_id from client1);
我使用access作为数据库,就像这样 search
答案 0 :(得分:1)
我的猜测是,下面的选择应返回所需的结果:
SELECT user_id,Max(time)AS [start], Min(time) AS [end]
FROM client1
WHERE user_id IN ('A', 'B', 'C', 'D', 'Any other value')
GROUP BY user_id
注意:强>
要为所有用户提取最长和最长时间 ,只需删除WHERE
子句:
SELECT user_id,Max(time)AS [start], Min(time) AS [end] FROM client1
GROUP BY user_id