cbmtrx Posted October 8, 2010 Share Posted October 8, 2010 I have a basic select statement: SELECT id AS prodId FROM products WHERE (title='kookaburra') ORDER BY RAND() LIMIT 0, 8; from a table containing id,title,description. But of those 8 random results I need to require one record be included IF it's title='emu'. Any way to do this with one SELECT statement or a fancy ORDER BY of some kind? I also tried a subselect but it threw an error... Seems like this should be straightforward enough but I'm a bit stumped. Ideally, I'd want 7 random results + the required one (if it exists), otherwise 8 random results. Any advice? (mysql version 4.1) Quote Link to comment https://forums.phpfreaks.com/topic/215420-get-rand-results-with-one-required-row/ Share on other sites More sharing options...
Pikachu2000 Posted October 8, 2010 Share Posted October 8, 2010 Google 'why order by rand is bad'. Quote Link to comment https://forums.phpfreaks.com/topic/215420-get-rand-results-with-one-required-row/#findComment-1120161 Share on other sites More sharing options...
cbmtrx Posted October 8, 2010 Author Share Posted October 8, 2010 Yes, on huge tables with hundreds of records, which is not an issue here...which is why I'm using it. But do you have a suggestion on the original question? Quote Link to comment https://forums.phpfreaks.com/topic/215420-get-rand-results-with-one-required-row/#findComment-1120185 Share on other sites More sharing options...
fenway Posted October 8, 2010 Share Posted October 8, 2010 Then get 8 random ones, union in the one you really want, and then throw out one of the original 8 if necessary. Quote Link to comment https://forums.phpfreaks.com/topic/215420-get-rand-results-with-one-required-row/#findComment-1120214 Share on other sites More sharing options...
cbmtrx Posted October 8, 2010 Author Share Posted October 8, 2010 ahh....UNION. Never used this much and will investigate! tx Quote Link to comment https://forums.phpfreaks.com/topic/215420-get-rand-results-with-one-required-row/#findComment-1120238 Share on other sites More sharing options...
cbmtrx Posted October 8, 2010 Author Share Posted October 8, 2010 OK, it seemed a little finicky but it totally works. To get 7 random items + 1 specific item: (SELECT id FROM products WHERE (title != 'something specific' AND category='marmalades') ORDER BY RAND() LIMIT 0, 7) UNION (SELECT id FROM products WHERE (title='something specific' AND category='marmalades') LIMIT 0, 1); I ended up having to force title != 'something specific' in the first SELECT and then force title = 'something specific' in the second one to achieve the desired result. Unfortunately this doesn't yet allow for getting only 8 random results if no 'something specific' exists but it's a workable solution. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/215420-get-rand-results-with-one-required-row/#findComment-1120255 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.