Forum Moderators: open

Message Too Old, No Replies

LEFT JOIN Problem

How to Use Left Join and ORDER

         

ariaby

8:31 pm on Jul 15, 2007 (gmt 0)

10+ Year Member



I have to tables 'users' and 'logs'
users: registered users information
logs: users activity logs

I want to select some users from 'users' table and last record of their activity from 'logs' table, there maybe [no] record or [many] records of activity in 'logs' table for users, so i think i should use left join

SELECT users.id,users.name,user_log.lad as user_activity FROM users LEFT JOIN logs on users.id=logs.user_id GROUP BY logs.user_id

I get list of users with their last activity date, but the problem is MySql selects the records in 'logs' table from the first records so i get old records instead of new ones

I need MySQL to select records from 'logs' table, starting from last record

Logs table:
----------------------------------------------
log_id ¦ user_id ¦ lad (Last activity) ¦
----------------------------------------------
1 ¦ 2 ¦ 2007-07-15 13:47:36 ¦
----------------------------------------------
2 ¦ 2 ¦ 2007-07-15 11:34:13 ¦
----------------------------------------------
3 ¦ 3 ¦ 2007-07-15 12:08:04 ¦
----------------------------------------------
4 ¦ 2 ¦ 2007-07-15 20:12:15 ¦

The result for my query will be:
user_id: 2, last_active:2007-07-15 13:47:36 (Record 1 in logs table)
But as you see the last record in logs table shows activity date of 2007-07-15 20:12:15 for user 2 (Record 4 in logs table)

Any idea how to fix this query?