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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:01:34+00:00 2026-05-21T17:01:34+00:00

I have a table that looks something like this: ________________________ |id|value|date|approved| ———————– What I

  • 0

I have a table that looks something like this:

________________________
|id|value|date|approved|
-----------------------

What I need to be able to do is get each row where approved = 1. That part is obvious. For each occurrence of value, I only want the most recent row (sorted by date).

Meaning that with a table like this:

________________________
|id|value|date|approved|
-----------------------
|1 |Foo  | 5  |   1    |
|2 |Bar  | 6  |   1    |
|3 |Foo  | 8  |   1    |
-----------------------

I only want the rows with id 2 and 3.

I assume I need to use DISTINCT somehow, but I’m not sure how. Could anyone help me out here?

  • 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-21T17:01:35+00:00Added an answer on May 21, 2026 at 5:01 pm
    SELECT  m.*
    FROM    (
            SELECT  DISTINCT value
            FROM    mytable
            ) md
    JOIN    mytable m
    ON      m.id =
            (
            SELECT  id
            FROM    mytable mi
            WHERE   mi.value = md.value
                    AND mi.approved = 1
            ORDER BY
                    mi.value DESC, mi.date DESC, mi.id DESC
            LIMIT 1
            )
    

    Create an index on (value, date, id) for this to work fast.

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

Sidebar

Related Questions

I have a table that looks something like this: word big expensive smart fast
I have a table that looks something like this: id | firstperson | secondperson
I currently have a table structure that looks something like this(some details omitted): ColumnName
I have a table that looks like this: id datatype name value 0 nvarchar(255)
I have a fairly complex query that looks something like this: create table Items(SomeOtherTableID
So I have a base table that looks something like this. SELECT [BILL_MONTH] ,[BILL_YEAR]
I have a mysql table that looks something like this: Row 1: 'visitor_input_id' =>
Let's say I have a table that looks something like this: ------------------------------- id|column2|column3 |column4
I have a MySQL table that looks something like this: |---ID---|---COUNTER---| | 1 |
I have a multi-row insert that looks something like: insert into table VALUES (1,

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.