sql - Is it possible to sort the results of a SELECT query in 2 different ways based on a conditional? -
the following invalid syntax i'm trying essentially:
select * categories order menu_order asc parent = '1' || if parent = '0' order id asc;
is possible order results of select query differently based on conditional within query?
just in case wasn't clear, trying order categories parent menu order , without parent id.
edit
so here's how database looks like:
id | name | parent | menu_order ================================== 1 | apparel | 0 | 2 2 | sports | 0 | 4 3 | hats | 1 | 5 4 | weights | 1 | 3 5 | lights | 0 | 1
the result set of query should be:
apparel sports lights weights hats
use case in order by, this:
select * categories order case when parent = '1' menu_order else id end;
Comments
Post a Comment