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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:26:29+00:00 2026-06-01T14:26:29+00:00

This question is related to my previous question – MySQL query to show records

  • 0

This question is related to my previous question – MySQL query to show records with current date on top and others as per descending order . The query i uses now is,

SELECT b.sales_id,b.category_id,b.sale_starts,b.sale_ends 
FROM tbl_sales b WHERE b.active=1 
UNION
SELECT b.sales_id,b.category_id,b.sale_starts,b.sale_ends 
FROM tbl_sales b INNER JOIN tb_category c ON  b.category_id=c.cat_id 
WHERE c.cat_keyword LIKE 'a' 
ORDER BY IF(sale_ends = DATE(NOW()), 0, 1), sale_ends DESC

and the results returned is as follows,

sales_id      | category_id         |sale_starts | sale_ends 
    ----------|---------------------|------------|--------------
     4        |   12                | 2012-04-05 | 2012-04-11 (today's date)
     1        |   10                | 2012-03-31 | 2012-04-30     
     2        |   11                | 2012-03-22 | 2012-04-27
     3        |   25                | 2012-03-31 | 2012-04-25
     5        |   18                | 2012-04-05 | 2012-04-09
     6        |   20                | 2012-02-23 | 2012-02-27
     7        |   14                | 2012-02-25 | 2012-02-26

But now i am stuck with another issue and i need the to sort the records like shown below –

    sales_id      | category_id         |sale_starts | sale_ends 
        ----------|---------------------|------------|--------------
         4        |   12                | 2012-04-05 | 2012-04-11 (today's date)
         3        |   25                | 2012-03-31 | 2012-04-25
         2        |   11                | 2012-03-22 | 2012-04-27
         1        |   10                | 2012-03-31 | 2012-04-30 
         7        |   14                | 2012-02-25 | 2012-02-26 (expired/past dates)
         6        |   20                | 2012-02-23 | 2012-02-27
         5        |   18                | 2012-04-05 | 2012-04-09

I had tried using ASC instead of DESC in the query but then the expired dates are listed just after the today’s date. I need the future dates to listed after the today’s date and after that only the expired dates. How can this be implemented?

Need help. Thanks in advance

  • 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-06-01T14:26:31+00:00Added an answer on June 1, 2026 at 2:26 pm

    ORDER BY will process each condition in order from left to right. So if the ordering you need is something like “Any entries with today’s date, then any entries in the future in ascending order of date, then other entries in ascending order of date” you can do something like this

    ORDER BY (sale_ends=CURDATE()) DESC,(sale_ends>CURDATE()) DESC,sale_ends ASC
    

    The reason the first two are listed as DESC is that the conditions will evaluate to one if true and zero if false. Since you want the true conditions first then you need to order them in DESCending order.

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

Sidebar

Related Questions

This question is related to a previous question of mine That's my current code
This question is related to this previous question: PDO, MySQL - How do I
This question is related to my previous post Image Processing Algorithm in Matlab in
This question is related to my previous question How to generate Cartesian Coordinate (x,y)
This question is related to my previous question . The storyline: I have an
This question is related to the previous post. How to save file and read
This question is related to a previous post . Is there something comparable to
This question is related with one of my earlier questions.. Previous Post In there
This is a follow-up question related to my previous post . Below is a
This is a CSS related question, I got one good answer from my previous

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.