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

  • Home
  • SEARCH
  • 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 9066797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:47:55+00:00 2026-06-16T16:47:55+00:00

I have a MySQL table with data, and would like to select the most

  • 0

I have a MySQL table with data, and would like to select the most recent entry for a specific test that was conducted.

table: research
columns: id (int), projectid (int), test (varchar),
         when (datetime), result (longtext)

Example of rows:

1, 6, "Test1", 01-01-2013 12:15, "AAAAA"
2, 6, "Test1", 01-01-2013 13:15, "BBBBB"
3, 6, "Test2", 01-01-2013 16:00, "CCCCC"
4, 6, "Test2", 01-01-2013 16:15, "DDDDD"
5, 6, "Test2", 01-01-2013 16:30, "EEEEE"

If I want the latest results for “Test 1”, I use..

SELECT *
FROM research
WHERE projectid=6 AND test='Test1'
ORDER BY when DESC limit 1

But how can I get the latest result for each test in a single project, in one query? I tried:

SELECT research.*
FROM research INNER JOIN (SELECT MAX(id) AS id
                          FROM research
                          WHERE projectid=".$ProjectId."
                          GROUP BY test) ids
     ON research.id = ids.id
WHERE research.projectid=".$ProjectId

But it does not give me the latest result from a test, it gives me the oldest..

  • 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-16T16:47:56+00:00Added an answer on June 16, 2026 at 4:47 pm

    The idea behind the subquery is that it separately gets the latest date WHEN for every projectid and test. The result of which is then joined back on the original table.

    SELECT  a.*
    FROM    research a
            INNER JOIN
            (
                SELECT projectid, test, MAX(`when`) maxDate
                FROM research
                GROUP BY projectid, test
            ) b ON  a.projectid = b.projectid AND
                    a.test = b.test AND
                    a.`when` = b.maxDate
    
    • SQLFiddle Demo
    • 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 contains some sensitive data that I would like to
I have an application that shows data from a MySQL table. Basically, my application
I have three mysql tables that I would like to extract some information from,
I have a MYSQL table like this: STORES (SPID, StoreID, ProductID); And data like
I have the following mysql table called test. lets say it has the data
I have a MySQL table with the following data (simplified): INSERT INTO `stores` (`storeId`,
I have a MySQL table with a tree data structure. The fields are _id
I have a mysql table field set as time type which stores data in
When showing data from a mysql table on an php page does it have
I have a MySQL query that: gets data from three tables linked by unique

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.