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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:31:08+00:00 2026-05-16T00:31:08+00:00

In MySQL, I have two tables with a 1:n relationship. Table items has products,

  • 0

In MySQL, I have two tables with a 1:n relationship.

Table items has products, whose state is kept in another table, like so :

items:

id |ref_num|name    |...
1  |0001   |product1|...
2  |0002   |product2|...

items_states :

id|product_id|state_id|date
1 |1         |5       |2010-05-05 10:25:20
2 |1         |9       |2010-05-08 12:38:00
3 |1         |6       |2010-05-10 20:45:12
...

The states table is not relevant and only relates the state_id to the state name and so on.

How can I get products where the latest state is the one I specify, one item per row?

Thank you

  • 1 1 Answer
  • 2 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-16T00:31:08+00:00Added an answer on May 16, 2026 at 12:31 am

    You may want to try the following:

    SELECT i.ref_num, i.name, s.latest_date
    FROM   items i
    JOIN   (
               SELECT   product_id, MAX(date) as latest_date
               FROM     items_states
               GROUP BY product_id
           ) s ON (s.product_id = i.id);
    

    If you want to return just one item, simply add a WHERE i.id = ? to the query.

    Test case:

    CREATE TABLE items (id int, ref_num varchar(10), name varchar(10));
    CREATE TABLE items_states (id int, product_id int, state_id int, date datetime);
    
    INSERT INTO items VALUES (1, '0001', 'product1');
    INSERT INTO items VALUES (2, '0002', 'product2');
    
    INSERT INTO items_states VALUES (1, 1, 5, '2010-05-05 10:25:20');
    INSERT INTO items_states VALUES (2, 1, 9, '2010-05-08 12:38:00');
    INSERT INTO items_states VALUES (3, 1, 6, '2010-05-10 20:45:12');
    

    Result:

    +---------+----------+---------------------+
    | ref_num | name     | latest_date         |
    +---------+----------+---------------------+
    | 0001    | product1 | 2010-05-10 20:45:12 |
    +---------+----------+---------------------+
    1 row in set (0.02 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two mysql tables, one containing details on cars and one containing all
Say I have an MSSQL table with two columns: an int ID column that's
When I have two mysql servers that have different jobs (holding different databases) but
I have a MySQL table with approximately 3000 rows per user. One of the
I have two issues both related to (I believe) my SQL Server setup. I
I am fairly new to MySQL and have a project in which I need
I'm writng a small application in PHP + MySQL and have come to the
Anyone knows how to get sum of number? For example i have mysql column
I have a MySQL query that returns a result with a single column of
I have a MySQL database behind a firewall which can only be accessed via

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.