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 7038633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:41:34+00:00 2026-05-28T01:41:34+00:00

I am totally stumped on what seems like a simple problem. Maybe I’m just

  • 0

I am totally stumped on what seems like a simple problem. Maybe I’m just confused because I’ve been thinking/googling/searching around about it for too long.

Consider the following table:

log_id (auto increment primary key)
domain_id (foreign key to "domains" table)
type (enum('notice','warning'))
message (varchar)

Executing the following select:

SELECT *
FROM logs
WHERE domain_id = 4

Would give me, say, 2 logs of the type ‘notice’ and 3 of the type ‘warning’.

How would I go about selecting only the logs with the highest ID of their type? Effectively giving me the latest log per domain_id and type.

Just describing it makes me feel stupid, but I can’t seem to get it right..

Thanks for your help.

Chris

Edit:
For future reference, in case you want to select all records from the first table and show the latest record of the joined table (in this case select all domains and their latest log record (if available)), simply wrap a select on the domains table around it:

SELECT 
  domains.*, 
  logs.*
FROM 
  domains
LEFT JOIN (
  SELECT 
    l.*
  FROM 
    logs l
  INNER JOIN (
    SELECT
      MAX(log_id) as maxid
    FROM
      logs
    GROUP BY
      domain_id
      type
  ) l3 ON l.log_id = l3.maxid
) l2 USING (domain_id)
  • 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-05-28T01:41:34+00:00Added an answer on May 28, 2026 at 1:41 am

    Join to a subquery first:

    select
        l.*
    from
        log l
        inner join (
        select
            max(log_id) as maxid
        from
            log
        group by
            domain_id,
            type
        ) l2 on
            l.log_id = l2.maxid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this school assignment that I've been working on and am totally stumped
I have been trying to learn Prolog, and am totally stumped on what the
Totally confused here. I have a PARENT UIViewController that needs to pass an NSMutableArray
Totally confused by this one... We have a WAMPServer installation set up, running a
I am a totally blind programmer who would like to learn Python. Unfortunately the
I am totally new to SQL . I have a simple select query similar
I'm a totally blind individual who would like to learn more of the theory
This seems like the simplest thing in the world and I'm ready to pull
I am totally stumped on this. I am putting together a script that checks
So I'm totally stumped by this one and tempted to call OS bug. I

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.