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:
explicitly sort first whether column
null
, then value:order product_condition null, field(...)
this works because
product_condition null
0 non-null
columns , 1null
columns; , in (default) ascending order, former come first.rely on fact
null
sorted last in descending orderings , reverse argumentsfield()
:order field(product_condition, ...) desc
Comments
Post a Comment