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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:28:06+00:00 2026-05-29T19:28:06+00:00

In the SQL statement below the results are returned out of order. If they

  • 0

In the SQL statement below the results are returned out of order. If they were in ascending or descending order it would make sense, but it seems there is no order here.

Any insights as to why the ORDER BY function isn’t sorting by the ‘date’ alias would be very much appreciated.

SQL Statement:

SELECT id, date, type 
    FROM (SELECT resume_id AS id, DATE_FORMAT( date_mod, '%M %e, %Y' ) AS date, 'resume' AS TYPE 
              FROM resumes 
              WHERE user_id = '$user_id' 
          UNION ALL 
          SELECT profile_id, DATE_FORMAT( date_mod, '%M %e, %Y' ) AS date, 'profile' 
              FROM profiles 
              WHERE user_id = '$user_id' 
          ORDER BY date DESC LIMIT 5) AS d1 
    ORDER BY date

Results:

Resume was updated on February 14, 2012
Resume was updated on February 15, 2012
Resume was updated on February 15, 2012
Resume was updated on February 9, 2012
Profile was updated on February 9, 2012
  • 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-29T19:28:08+00:00Added an answer on May 29, 2026 at 7:28 pm

    It’s sorting them as strings because you’ve converted the dates to strings using DATE_FORMAT (note that as a string “February 15…” is lower than “February 9…” because 1 comes before 9 in the “alphabet”). The solution is to sort by the real date in date_mod. You could do it directly by just adding in date_mod to the selects and changing the order, like this:

    SELECT id, date, date_mod, type
    FROM (
      SELECT resume_id AS id,
        DATE_FORMAT(date_mod, '%M %e, %Y') AS date,
        date_mod,
        'resume' AS type
      FROM resumes
      WHERE user_id = '$user_id'
      UNION ALL
      SELECT profile_id AS id,
        DATE_FORMAT(date_mod, '%M %e, %Y') AS date,
        date_mod,
        'profile' AS type
      FROM profiles
      WHERE user_id = '$user_id'
      ORDER BY date_mod DESC
      LIMIT 5
    ) AS d1
    ORDER BY date_mod
    

    But even better would be to simplify it by selecting only date_mod in the sub-query (i.e. no formatted version) and doing the DATE_FORMAT last in the outer query:

    SELECT id, DATE_FORMAT(date_mod, '%M %e, %Y') AS date, type
    FROM (
      SELECT resume_id AS id, date_mod, 'resume' AS type
      FROM resumes
      WHERE user_id = '$user_id'
      UNION ALL
      SELECT profile_id AS id, date_mod, 'profile' AS type
      FROM profiles
      WHERE user_id = '$user_id'
      ORDER BY date_mod DESC
      LIMIT 5
    ) AS d1
    ORDER BY date_mod
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to call the SQL statement below but get the following error: System.Data.SqlClient.SqlException:
I want to write an SQL statement like below: select * from tbl where
I have a problem with the SQL statement detailed below. The query returns the
I'm trying to do the below sql statement in GORM select * from table1
I'm getting the error below for this SQL statement in VB.Net 'Fill in the
SQL statement: INSERT INTO order (`ORDER_ID`,`SALE_CODE`,`CREATED_AT`,`UPDATED_AT`) VALUES ('2646253286','HPHS20','2009-07-11 12:07:40','2009-07-11 12:07:40') Error: You have an
I am using wordpress and I have an SQL statement (as below) to gather
I am working on a SQL Statement that I can't seem to figure out.
Upon running the below SQL statement (which works perfectly) I've found that all tables
How do I loop through the results of a SELECT statement in SQL? My

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.