php - mysql sorting single field in multi orders -
hi trying sort field multiple orders example here table
1st rule sort status. in following order: active, inactive, merged, promo a, promo b, promo c, promo d, defunct
once sorted within each of these categories 2nd rule in following order: traditional, native, salvation, amm, nav
3rd rule sort state within these categories alphabetical a-z
if cannot in mysql can in php? sort function
i need idea or trick how can
-- ---------------------------- -- table structure `users` -- ---------------------------- drop table if exists `users`; create table `users` ( `user_id` int(11) not null auto_increment, `name` varchar(255) default null, `status` varchar(255) default null, `category` varchar(255) default null, `state` varchar(255) default null, primary key (`user_id`) ) engine=myisam auto_increment=11 default charset=utf8; -- ---------------------------- -- records of users -- ---------------------------- insert users values ('1', 'testing', 'inactive', 'traditional', 'az'); insert users values ('2', 'testing 2', 'merged', 'native', 'ak'); insert users values ('3', 'testing 3', 'promo a', 'salvation', 'dc'); insert users values ('4', 'testing 4', 'promo b', 'salvation', 'ph'); insert users values ('5', 'testing 5', 'promo c', 'amm', 'po'); insert users values ('6', 'testing 6', 'promo d', 'mai', 'az'); insert users values ('7', 'testing 7', 'defunct', 'nav', 'ak'); insert users values ('8', 'test 8', null, 'abc', 'dd'); insert users values ('9', 'test 9', 'asd', null, null); insert users values ('10', 'test 10', 'active - full membership', null, null);
use case
select * users order ( case status when 'active' 1 when 'inactive' 2 when 'merged' 3 when 'promo a' 4 when 'promo b' 5 when 'promo c' 6 when 'promo d' 7 when 'defunct' 8 else 9999 end ), ( case category when 'traditional' 1 when 'native' 2 when 'salvation' 3 when 'amm' 4 when 'nav' 5 else 9999 end ), state;
Comments
Post a Comment