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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:04:44+00:00 2026-06-17T08:04:44+00:00

I am working with sort syntax for mysql db and i would like to

  • 0

I am working with sort syntax for mysql db and i would like to sort the rows by completion of columns. I hope example will describe the issue:

id |t1 | t2 | t3 | t4
----------------------
 1 | a |  b |  c | d
 2 |   |  e |    |
 3 | f |  g |    |
 4 | h |  i | j  |

this is my row/column structure. As you can see, some of the fields are empty. I’d like to sort the table so the imput would look like this

id |t1 | t2 | t3 | t4
----------------------
 1 | a |  b |  c | d
 4 | h |  i | j  |
 3 | f |  g |    |
 2 |   |  e |    |

It’s been sorted by number of filled (non-empty) fields. The easiest way to do this is to sort with such a query:

SELECT id, t1, t2, t3, t4 FROM table ORDER BY t1 DESC, t2 DESC, t3 DESC, t4 DESC

It works good in most of cases but look what will happen if my table looks like this:

id |t1 | t2 | t3 | t4
----------------------
 1 |   |  b |  c | d
 2 |   |  z |    |

After sorting:

id |t1 | t2 | t3 | t4
----------------------
 2 |   |  z |    |
 1 |   |  b |  c | d

Why? Because sort order is descending, and t1 column is empty (so it’s ignored), and the next column is t2. It is also sorted descending ignoring rest of the columns (if they are empty).

I found similiar problem solved (i guess – not checked yet) here but the solution looks nasty. Do you have other ideas to sort the table by number of non-empty columns?

  • 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-17T08:04:45+00:00Added an answer on June 17, 2026 at 8:04 am

    You should sum the results of IS NULL or IS NOT NULL for all of the relevant columns and use that as a primary sort to solve your basic problem, then impose whatever secondary sort you want.

    It’s unclear to me how you want to sort two rows with the same number of non-null column values, but I’ll guess that you want to sort them ascending, left to right, in which case this should work for you:

    (UPDATE: as eggyal pointed out, by “empty” you mean the empty string, not NULL. See updated query below):

    SELECT id, t1, t2, t3, t4 
    FROM table 
    ORDER BY ((t1 = '') + (t2 = '') + (t3 = '') + (t4 = '')) ASC, 
    t1 DESC, t2 DESC, t3 DESC, t4 DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on this application that's sort of like a blog. I'm thinking about
I'm working on some sort of online multiuser editor / coop interface, which will
I have been working on this sort of ATM (With a maximum of 50
I am currently working on a sort of image processing tool that will contain
I'm working on a program that will sort files based on extension I currently
I'm working on some sort of online indesign editor and i ran into the
I'm working on a sort of proof-of-concept for a project that approximates Firebug's inspector
I've managed to get my pthreads program sort of working. Basically I am trying
Can someone clearly explain me how these functions of heap sort are working?? void
With the sort of programs I write (working with raw file data) I often

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.