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! Quote Link to comment 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] Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted January 12, 2007 Author Share Posted January 12, 2007 now that's cool 8) Quote Link to comment Share on other sites More sharing options...
effigy Posted January 12, 2007 Share Posted January 12, 2007 Touché Barand. Quote Link to comment 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.