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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:43:21+00:00 2026-06-12T12:43:21+00:00

I am doing a sorting on a table to show the records with status

  • 0

I am doing a sorting on a table to show the records with status having 3 first then status having 4 and then 1.

in current table this is the output

id    status
 1     3
 2     4
 3     4
 4     3
 5     1

now when i apply the query

select * from table order by model.status desc 

the output is:

id    status
 2     4
 3     4
 1     3
 4     3
 5     1

what i want actually the below output. first with status 3 then with status 4 and then with status 1. How to achieve the below output

id    status
 1     3
 4     3
 2     4
 3     4
 5     1
  • 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-12T12:43:23+00:00Added an answer on June 12, 2026 at 12:43 pm

    instead of a magic 9999999 number, you can use ~0, which represents the maximum possible value for big int… should be enough 😉

    order by (case when status = 1 then ~0 else status end) 
    

    other solution, without any magic number

    order by status = 1 , status
    

    which will sort by a “boolean” (0 or 1 in DB) first, then by status

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

Sidebar

Related Questions

I am displaying the database table values using data tables. I am doing this
I'm doing sorting based on a field that references another table (and sorting on
I've written a few short recursive programs, and am now doing sorting recursively. I've
I am thinking of sorting and then doing binary search. Is that the best
In class we are doing sorting algorithms and, although I understand them fine when
I'm doing a sorting of a set of users, I have 4 groupings like
Doing the getting started of Sinatra. I get this error: ./sinatra.rb:5: undefined method `get'
Doing my first SL4 MVVM RIA based application and i ran into the following
Doing my first tryouts with foreign keys in a mySQL database and are trying
Doing a mysqli query and COUNTing the results, is there a preferred comparison operator

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.