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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:00:36+00:00 2026-05-24T02:00:36+00:00

I have a MySQL query that produces this output ordered like this: Instead I’d

  • 0

I have a MySQL query that produces this output ordered like this:

current output

Instead I’d like the output ordered like this:
desired output

I’m not sure how to write the query so that it orders the data the way I need it. I need the data sorted by date DESC, then by time DESC, but also to chunk common ‘markers_name’ elements together. (I only want them chunked together for each date).

This is the query I’m using:

SELECT markers.name AS markers_name, 
       conditions.time AS conditions_time, 
       conditions.date AS conditions_date, 
       station.name AS station_name 
FROM markers, conditions, users 
WHERE conditions.markers_id = markers.id 
AND (conditions.station_id = station.id)
ORDER BY date DESC, markers.name, time DESC 
LIMIT 100

(P.S. I omitted showing the station_name in my output tables, but they do show up in my real query.)

EDIT:
I’ve added some more tables in an attempt to explain the problem better.
Note that “chunk” isn’t really returned by the query. I just added that to help with the conversation. Each chunk has a common date.

Here is output after I tried ORDER BY date DESC , markers.name DESC , time DESC
Desired output

And here is my desired output:

enter image description here

Compare ‘Chunk 4’ in the two outputs. The upper table orders by name descending. This is not desired. Marker_name ‘d’ has the newest information so I want it to be first. ‘C’ has the next newest information. ‘f’ has the oldest information. The next table shows the ordering that I need.

Hopefully this conveys the issue better. Can anyone help?

EDIT 2
I tried this proposed answer: “ORDER BY date DESC, time DESC, markers.name DESC” but it didn’t work.

order by date, time, name

Note that chunk 3 is ordered by time. The ‘e’ and ‘b’ rows are not grouped together.

  • 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-24T02:00:36+00:00Added an answer on May 24, 2026 at 2:00 am

    Get the list of MAX(time) values for every combination of (markers_id, date) in conditions, then join that list to the rowset you are getting from your present query, and use the MAX(time) values for sorting:

    SELECT
      m.name AS markers_name,
      c.time AS conditions_time,
      c.date AS conditions_date,
      s.name AS station_name
    FROM markers m
      INNER JOIN conditions c ON c.markers_id = m.id
      INNER JOIN station s    ON c.station_id = s.id
      INNER JOIN (
        SELECT
          markers_id,
          date,
          MAX(time) AS time
        FROM conditions
        GROUP BY
          markers_id,
          date
      ) mx ON c.markers_id = mx.markers_id AND c.date = mx.date
    ORDER BY
      c.date DESC,
      mx.time DESC,
      m.name DESC,
      c.time DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL query like this: SELECT *, SUM(...some SQL removed for brevety)
I have data in an MYSQL database that looks like this: Project Date Time
I have a MySQL query that returns a result with a single column of
I have a MySQL query that I use to retrieve random rows from a
I have a MySQL (v 5, MyISAM) query that returns different rows depending on
So I have written a query that will grab an order (this is for
I have three tables I'd like to join in a way that produces all
I have a query that simply fetch data from my database, this query returns
I have a mysql query that is trying to combine the following tables: TABLE:
I'm trying to write query to a MYSQL database that queries for products that

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.