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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:47:56+00:00 2026-05-13T07:47:56+00:00

For example, I have a shop order database, and two tables in it –

  • 0

For example, I have a shop order database, and two tables in it – ORDERS and ORDERSTATUS.

Table : orders
--------------------------------------------
OrderID | OrderItems | AddedTimeStamp      |
--------------------------------------------
1       | Apples     | 2009-12-22 13:15:18 |
--------------------------------------------
2       | Bananas    | 2009-12-22 14:15:24 |
--------------------------------------------

Table : orderstatus
--------------------------------------------------------------------
StatusID | OrderID | Status   | AssignedUser | StatusTimestamp     |
--------------------------------------------------------------------
1        | 1       | Received | JohnSmith    | 2009-12-22 14:15:24 |
--------------------------------------------------------------------
2        | 2       | Received | MaryJane     | 2009-12-22 14:15:24 |
--------------------------------------------------------------------
3        | 1       | Process  | JohnSmith    | 2009-12-22 14:15:24 |
--------------------------------------------------------------------
4        | 2       | Process  | MaryJane     | 2009-12-22 14:15:24 |
--------------------------------------------------------------------
5        | 2       | Deliver  | MaryJane     | 2009-12-22 14:15:24 |
--------------------------------------------------------------------

I am running this SQL query :

SELECT od.orderid, od.orderitems, os.status, os.assigneduser
FROM orders AS od INNER JOIN orderstatus AS os
ON od.orderid = os.orderid
GROUP BY os.orderid
ORDER BY os.orderid ASC

This returns me :

------------------------------------------------
OrderID | OrderItems | Status   | AssignedUser |
------------------------------------------------
1       | Apples     | Received | JohnSmith    |
------------------------------------------------
2       | Bananas    | Received | MaryJane     |
------------------------------------------------

What I would like is :

------------------------------------------------
OrderID | OrderItems | Status   | AssignedUser |
------------------------------------------------
1       | Apples     | Process  | JohnSmith    |
------------------------------------------------
2       | Bananas    | Deliver  | MaryJane     |
------------------------------------------------

I’m quite new to MySQL queries, but I’ve been banging my head for the past 4 hours – can someone help?? TIA.

EDIT : The basic objective is I want to show the latest status for the orders.

  • 1 1 Answer
  • 1 View
  • 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-13T07:47:56+00:00Added an answer on May 13, 2026 at 7:47 am

    Its tough to get what you want without more information. All of the records in the orderstatus table have the same timestamp. It seems you just want the ones with the other status, but there isn’t any way to differentiate the different statuses.

    In order to do this, we need to tell MySQL how to pick the best row. Look closely… How would you know if you were a machine, which row to return?

    The best I can guess is that you want the latest orderstatus record (max statusid). If that is the case, then here is a script that will work:

    SELECT
        od.orderid, 
        od.orderitems, 
        os.status, 
        os.assigneduser
    FROM orders AS od
    INNER JOIN orderstatus AS os ON od.orderid = os.orderid
    INNER JOIN (
        SELECT
            MAX(StatusID) AS StatusID,
            OrderId
        FROM orderstatus
        GROUP BY OrderId
    ) as maxos ON maxos.StatusID = os.StatusID
    GROUP BY os.orderid
    ORDER BY os.orderid ASC
    

    I’m assuming your example is not accurate, and that the timestamp will actually be different in real life.

    In that case, you can use the same script from above, but get the max(timestamp) instead of max(statusid).

    The other thing you can do is normalize the status column (create a table for status codes, and add a column designating the order. Then you can get the max(that new order column) using the script above.

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

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You mean like this? http://www.codinghorror.com/blog/2005/10/c-implementation-of-ascii85.html May 14, 2026 at 10:28 pm
  • Editorial Team
    Editorial Team added an answer As you can see, it's cutting everything off the url… May 14, 2026 at 10:28 pm
  • Editorial Team
    Editorial Team added an answer Got it. You can't create the objects directly, but d2d1.dll… May 14, 2026 at 10:28 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.