php - mysql order by field with NULL values last -


i have been having issue when using field function in order clause.

my situation product can have 3 categories , user can choose category show first. there 3 possible queries can formed. these are:

query 1

select      *     my_table     main_categories_id = 2 order field(product_condition,         'graded',         'new',         'used'); 

query 2

select      *     my_table     main_categories_id = 2 order field(product_condition,         'new',         'graded',         'used'); 

query 3

select      *     my_table     main_categories_id = 2 order field(product_condition,         'used',         'new',         'graded'); 

this not work when product condition null because shows rows null value first. need these appear last.

i have tried adding null field function doesnt seem work.

does know way can achieve this?

thanks help.

you can either:

  1. explicitly sort first whether column null , then value:

    order product_condition null, field(...) 

    this works because product_condition null 0 non-null columns , 1 null columns; , in (default) ascending order, former come first.

  2. rely on fact null sorted last in descending orderings , reverse arguments field():

    order field(product_condition, ...) desc 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -