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.