Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7763153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:35:37+00:00 2026-06-01T14:35:37+00:00

I have two tables user and log. I would like to join the user

  • 0

I have two tables user and log. I would like to join the user table with logs and get the latest log entry. I have noted that using the group by the rows inside the group are not sorted. Therefore I am not able to retrieve the latest log row.

USER:

 ID    |   Name
--------------------
  1    |   John    
  2    |   Mike    

LOG:

L_ID | ACTION  | DATE     | USERID
----------------------------------
1    | OPEN    | '1 DEC'  |   1
2    | CLOSE   | '3 DEC'  |   1
3    | WRITE   | '2 DEC'  |   1
4    | OPEN    | '5 DEC'  |   2
5    | CLOSE   | '3 DEC'  |   2

With this table the result I would like to have is:

2 Mike 4 OPEN  '5 DEC'
1 Jhon 2 CLOSE '3 DEC'

I know that the following query is not working since rows inside the group are not sorted:

Select * from user join log on ID=USERID group by ID order by DATE DESC,L_ID

Also the following is not working since only the correct date is returned but not the actions:

Select ID,Name,Action,Max(Date) from user join log on ID=USERID group by ID order by DATE DESC, L_ID

Any idea suggestion how to write the query that will return the correct result?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:35:39+00:00Added an answer on June 1, 2026 at 2:35 pm

    This is pretty straightforward:

    SELECT u.*, l.* 
    FROM user AS u 
    INNER JOIN log AS l ON l.USERID=u.ID 
    WHERE l.DATE = (SELECT max(DATE) FROM log WHERE l.USERID=u.ID ORDER BY L_ID DESC LIMIT 1)
    
    2 Mike 4 OPEN  '5 DEC'
    1 Jhon 2 CLOSE '3 DEC'
    

    This should work in MySQL, hence sqlite too hopefully.. This may not be the best optimized, I hope to see someone more knowledgeable giving the exact query.

    This supposes your date field is proper date time field and not strings. These links might give you some more idea.

    Please also note that ordering by ID is not safe, it need not be always first record, lesser id.. it depends. So always have a safe second sort column, something like weight or so which stands for ‘time’ or your own custom rank.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables User & User Log . User log table basically logs
I have two tables in my database: user and media_contact . The media_contact table
I have two tables, stats and stat_log. The stat log is like this: user_id,stat_id,value,registered
I have two tables, user and company, and I have a combo box in
I have two tables, users and hra. A user can only have one record
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)
Imagine we have two tables in the database, user (FK id_role) and role (PK
I've got the following problem: I have two tables: (simplified) +--------+ +-----------+ | User
My database (sqlite3), has two tables with the following schemas: CREATE TABLE log(d date,
Say I have two tables User ----- id first_name last_name User_Prefs ----- user_id pref

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.