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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:38:00+00:00 2026-06-12T01:38:00+00:00

Want to sort Nulls and Blanks last and have read all the solutions that

  • 0

Want to sort Nulls and Blanks last and have read all the solutions that use either the Coalesce function or the If in the order by column.

MY problems is non of these work for me because the column I am sorting on is specified dynamically by the PHP script that creates the query and some of my columns are in two tables in my join.

   $sortcol="boat";
   $sql= "SELECT fleet,b.boat as boat,owner FROM boats as b 
   LEFT JOIN owners as o ON  b.boat=o.boat 
   ORDER BY $sortcol";

This works great, I can change the variable $sortcol and my output listing works great except the nulls and blanks are at the top.

Based on other postings I tried this

   $sortcol="boat";
   $sql= "SELECT fleet,b.boat as boat,owner FROM boats as b 
   LEFT JOIN owners as o ON  b.boat=o.boat 
   ORDER BY IF($sortcol is NULL,1,0), $sortcol";

This throws the error “column boat in ORDER BY clause is ambiguous”. Obviously it wants b.boat in the order by, but for reasons I won’t get into that is problematic.
It appears that anytime I try to use a function in the orderby clause I can’t use the column alias.

Any ideas for an elegant solution to this??

  • 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-12T01:38:02+00:00Added an answer on June 12, 2026 at 1:38 am

    You’re right. For no reason that I can fathom, MySQL accepts an ambiguous ORDER BY as long as the name you supply is not handled in any way (no way that I could think of. Maybe others exist).

    As soon as it is, ambiguousness gets rejected.

    This is accepted (and redundant):

    select b.id, a.name as name
        FROM client AS a JOIN client AS b ON (a.id = b.id)
        ORDER BY name, name;
    

    while COALESCE(name, ''), name IS NULL, name OR NULL are all rejected.

    The obvious solution is to use a different name for the alias, one that does not appear in either table.

    Another possibility would be to create a nested query:

    SELECT * FROM ( your query here, without ORDER ) AS original
    ORDER BY IF($sortcol is NULL,1,0), $sortcol;
    

    That is:

    $sortcol="boat";
    $sql = <<<SQL
       SELECT * FROM (
          SELECT fleet,b.boat as boat,owner FROM boats as b 
             LEFT JOIN owners as o ON  b.boat=o.boat 
       ) AS original
       ORDER BY IF($sortcol is NULL,1,0), $sortcol;
    SQL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to sort the items that have been previously selected with checkboxes. If
I have a query that I want to sort alphabetically, but the trick is
I want to sort the Dictionary value following is my dictionary response that have
Question: I want to use a VBA function in Access that is declared as
I have a linked list, and I want to sort them by names (for
I have a LINQ to object query to select all the persons that are
I have a rest application that returns json/xml. I use jackson and jaxb for
I have a problem understandint ORDER BY in MySQL . I have to sort
When the user does a column sort in my DataGrid , I want all
I want to sort an array by using uasort() function. I wrote this piece

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.