michaellunsford Posted January 12, 2007 Share Posted January 12, 2007 Imagine searching for a product. I want to see all "HP" brand products first, but after all the HP stuff, the other brands shouldn't be put in any particular order.I'm thinking it would be equivalent to two separate queries: one with `field`='arbitrary_value' and the next `field` != 'arbitrary_value'. Writing two separate queries into the php page would be a pretty dramatic code rewrite, though. Is there a MySQL way to do it?Thanks! Link to comment https://forums.phpfreaks.com/topic/33905-arbitrary-sort/ Share on other sites More sharing options...
effigy Posted January 12, 2007 Share Posted January 12, 2007 I'm not sure if this is the best way, but what about a UNION?[tt](select * from products where brand = 'HP' order by name)union(select * from products where brand != 'HP' order by rand())[/tt] Link to comment https://forums.phpfreaks.com/topic/33905-arbitrary-sort/#findComment-159176 Share on other sites More sharing options...
michaellunsford Posted January 12, 2007 Author Share Posted January 12, 2007 union... That's a new one on me... 8) Works great. That's one I'll have to write down.hmmm... no "solved" link. Link to comment https://forums.phpfreaks.com/topic/33905-arbitrary-sort/#findComment-159185 Share on other sites More sharing options...
Barand Posted January 12, 2007 Share Posted January 12, 2007 Another way is to create a boolean result to sort onselect * from products order by brand='HP' DESC, name Link to comment https://forums.phpfreaks.com/topic/33905-arbitrary-sort/#findComment-159472 Share on other sites More sharing options...
michaellunsford Posted January 12, 2007 Author Share Posted January 12, 2007 now that's cool 8) Link to comment https://forums.phpfreaks.com/topic/33905-arbitrary-sort/#findComment-159477 Share on other sites More sharing options...
effigy Posted January 12, 2007 Share Posted January 12, 2007 Touché Barand. Link to comment https://forums.phpfreaks.com/topic/33905-arbitrary-sort/#findComment-159480 Share on other sites More sharing options...
fenway Posted January 12, 2007 Share Posted January 12, 2007 Yeah... using ORDER BY FIELD() is also a nifty trick... Link to comment https://forums.phpfreaks.com/topic/33905-arbitrary-sort/#findComment-159560 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.