用下面的语句可以实现你要的功能,先排序再自关联
select b.username,b.loginTime FROM ( SELECT a.username,a.loginTime FROM UserDB.dbo.LogInfo a where a.kid IN ( select TOP 3 kid from UserDB.dbo.LogInfo where a.username = username ORDER BY a.loginTime DESC ) ) b group BY b.username,b.loginTime ORDER BY b.username
你好,我试了试,测试结果是:每组只能显示1条记录。
试试这,没有验证的
1 SELECT a.kid,a.username,a.loginTime FROM login_log a 2 WHERE 3>=( 3 SELECT COUNT(*) FROM login_log b 4 WHERE a.kid=b.kid) 5 ORDER BY a.username DESC
1 SELECT DISTINCT kid, username, loginTime FROM login t1 2 WHERE kid in ( 3 SELECT t2.kid FROM login t2 4 WHERE t2.username = t1.username 5 ORDER BY t2.loginTime DESC 6 LIMIT 3)
7 ORDER BY username;