简体   繁体   中英

MySQL - ORDER BY date, then collate by title

I wish to sort my results into groups (but not GROUP BY , just collated) of column title, and within each group they would be sorted by date. The groups would be ascending in date.

I thought a simple ORDER BY date, title would suffice, but this does not work as expected.

You can see my data here: http://sqlfiddle.com/#!2/658f7c/11 The rows with the message column containing Old (...) should appear first, while the ones containing New (...) should appear later (within the groups, they should be sorted by date).

The expected output should be below. Note: the date should always be ascending.

Title  |  Message     | Date
-----------------------------
Old    |  Old (One)   | 0001
Old    |  Old (Two)   | 0002
Old    |  Old (Three) | 0029
New    |  New (One)   | 0002
New    |  New (Two)   | 0003
New    |  New (Three) | 0004

Try this:

SELECT title, message, post_id, user_id, post_date
FROM `table`
ORDER BY title DESC, post_date ASC

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM