3

I have 2 tables : User and LoginLogs and User and LoginLog have 'has Many' relationships. I have to get last login of user from the LoginLog table. I have to query the details of last login user using group by. Here is the Query:

SELECT * FROM `login_logs` 
AS `LoginLog` 
LEFT JOIN `users` 
AS `User` 
ON (`LoginLog`.`user_id` = `User`.`id`) 
WHERE 1 = 1 GROUP BY `LoginLog`.`user_id` 
ORDER BY `LoginLog`.`login_datetime` DESC LIMIT 20

This Query returns first login record.

4

2 に答える 2

-1

疑似コードで:

 SELECT * FROM LoginLog 
 WHERE MAX(LoginLog.primary_id) AND user.id = loginLog.userId

LoginLogに自動インクリメントPK列があると仮定します

パフォーマンスを向上させるために、userId 列のインデックスを作成することを忘れないでください

于 2013-04-11T07:44:12.667 に答える