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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:41:55+00:00 2026-06-06T06:41:55+00:00

I have one mysql query, which gets rows to designs list. In this list

  • 0

I have one mysql query, which gets rows to designs list. In this list we have “normal” rows, which are sorted by their id, and “special” rows which are sorted by “special_end” (this is date, in all rows it’s NULL now) parameter. So, “special” rows in result should be higher than “normal”.

Query now:

SELECT d.*, DATE_FORMAT(_created, '%d.%m.%Y') as _created_human, count(dp.id) photos_cnt, u.id user_id, u.class_name user_class_name, u.full_name user_full_name
FROM designs d
LEFT JOIN design_photo dp ON d.id=dp.design_id 
LEFT JOIN cms_user u ON d.cms_user_id=u.id
WHERE 1=1  AND d.status_id=1 AND d.published=1 AND u.cms_user_status_id=1 
GROUP BY d.id
ORDER BY d.special_end DESC, id DESC 
LIMIT 20 OFFSET 0

I need to upgrade ORDER BY block in this query to sort rows with special_end parameter with condition:

  • if d.special_end >= DATE( NOW() ) -> sort by d.special_end DESC,
  • if d.special_end <= DATE( NOW() ) -> sort by id DESC.

Example (id – row – special_end):

515 design4 2012-07-21
514 design3 2012-06-30
526 design5 null
513 design2 2012-05-30 (this date is <= DATE( NOW() ))
512 design1 null
511 design0 null
.
.
.

I’ve already try to write something like

ORDER BY CASE d.special_end WHEN d.special_end >= DATE( NOW() ) THEN d.special_end WHEN d.special_end <= DATE( NOW() ) THEN d.id END DESC

or

ORDER BY IF(d.special_end >= DATE( NOW() ), d.special_end, d.id) DESC

but no result.

Thanks for your help.

  • 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-06T06:41:57+00:00Added an answer on June 6, 2026 at 6:41 am

    Ordering by a condition might be terribly slow, I’d rather use UNION to achieve the same result:

    SELECT t.* FROM (
        (
            SELECT d.*, DATE_FORMAT(_created, '%d.%m.%Y') as _created_human, count(dp.id) photos_cnt, u.id user_id, u.class_name user_class_name, u.full_name user_full_name
            FROM designs d
            LEFT JOIN design_photo dp ON d.id=dp.design_id 
            LEFT JOIN cms_user u ON d.cms_user_id=u.id
            WHERE d.special_end >= DATE(NOW()) AND d.status_id=1 AND d.published=1 AND u.cms_user_status_id=1 
            GROUP BY d.id
            ORDER BY d.special_end DESC
        ) UNION (
            SELECT d.*, DATE_FORMAT(_created, '%d.%m.%Y') as _created_human, count(dp.id) photos_cnt, u.id user_id, u.class_name user_class_name, u.full_name user_full_name
            FROM designs d
            LEFT JOIN design_photo dp ON d.id=dp.design_id 
            LEFT JOIN cms_user u ON d.cms_user_id=u.id
            WHERE (d.special_end < DATE(NOW()) OR d.special_end IS NULL) AND d.status_id=1 AND d.published=1 AND u.cms_user_status_id=1 
            GROUP BY d.id
            ORDER BY d.id DESC
        )
    ) t LIMIT 20 OFFSET 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one main array which is the returned data from a MySQL query
Hi i have a small query which takes data from one mysql database and
I need to have MySQL query like this one: UPDATE table_name SET 1 =
I have a MySQL insert query which needs to pull one data field from
I have a MySQL query that only crashes on one certain where clause, it
I have a mysql query that uses union to join multiple queries into one
suppose i have one column in mysql database which is stated last time one
I have a big mysql query which needs to trawl through 4 tables to
I want to do a MySQL Query which selects a given number of Rows
I have a mySql query which adds certain interval of time to datetime field.

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.