2

在下面的mysql查询上出现错误

无效的组功能使用

SELECT logtime 
from  sgwebdb.audit_log 
where logtime < MAX(logtime) 
  AND userid = 'admin' 
  AND activity = 'LOGIN';
4

3 回答 3

1

我认为上面的查询没问题,但是:

SELECT logtime 
from  sgwebdb.audit_log L1
where logtime < (SELECT MAX(L2.logtime) FROM sgwebdb.audit_log L2 where L2.userid = L1.userid 
    AND L2.activity = L1.activity) 
    AND userid = 'admin' 
    AND activity = 'LOGIN';

我认为您希望这两个信息用户/活动的最大登录时间。

于 2013-07-18T08:34:48.600 回答
1

试试这个:

SELECT logtime 
from  sgwebdb.audit_log 
where logtime < (SELECT MAX(logtime) FROM sgwebdb.audit_log ) 
  AND userid = 'admin' 
  AND activity = 'LOGIN';

更新

对于第二个最大值,您可以这样做:

SELECT logtime
FROM 
(
   SELECT logtime 
   FROM sgwebdb.audit_log
   WHERE userid = 'admin' 
    AND activity = 'LOGIN'
   ORDER BY logtime DESC
   LIMIT 2
) AS t
ORDER BT logtime 
LIMIT 1;
于 2013-07-18T08:31:49.117 回答
0

您正在尝试group function (MAX)在正常查询中使用。这是不正确的。如果您只想获得第二个最大值,那么

SELECT logtime 
from  sgwebdb.audit_log 
where logtime < (SELECT MAX(logtime) FROM sgwebdb.audit_log ) 
  AND userid = 'admin' 
  AND activity = 'LOGIN' limit 1;
于 2013-07-18T08:36:23.540 回答