wardo Posted May 15, 2006 Share Posted May 15, 2006 Hi,I need help with a query. At the moment I have a query which displays the results and orders them with the sponsored listings at the top of the page. What I want is for the sponsors to remain at the top of the page but everything to be displayed randomly as well, otherwise the people who have the lowest id will always be at the top of the search results.My current query is:"SELECT * FROM members where (city like '%$city%' or country like '%$country%') and approved = '1' ORDER BY sponsor DESC LIMIT $from, $max_results")Hope this makes sense [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /] Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/9710-need-help-with-an-order-by-query/ Share on other sites More sharing options...
ryanlwh Posted May 15, 2006 Share Posted May 15, 2006 i hope this is what you meant[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]members[/color] [color=green]where[/color] (city [color=orange]like[/color] [color=red]'%$city%'[/color] [color=blue]or[/color] country [color=orange]like[/color] [color=red]'%$country%'[/color]) [color=blue]and[/color] approved [color=orange]=[/color] [color=red]'1'[/color] [color=green]ORDER BY[/color] sponsor DESC, RAND() LIMIT $from, $max_results [!--sql2--][/div][!--sql3--]This will create a random sub-order within the sponsor ordering. Quote Link to comment https://forums.phpfreaks.com/topic/9710-need-help-with-an-order-by-query/#findComment-36008 Share on other sites More sharing options...
wardo Posted May 16, 2006 Author Share Posted May 16, 2006 Thanks,Thats almost it but not quite.I need the other results to be random as well as the sponsors.Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/9710-need-help-with-an-order-by-query/#findComment-36172 Share on other sites More sharing options...
fenway Posted May 16, 2006 Share Posted May 16, 2006 [!--quoteo(post=374216:date=May 16 2006, 04:20 AM:name=wardo)--][div class=\'quotetop\']QUOTE(wardo @ May 16 2006, 04:20 AM) [snapback]374216[/snapback][/div][div class=\'quotemain\'][!--quotec--]Thanks,Thats almost it but not quite.I need the other results to be random as well as the sponsors.Any other ideas?[/quote]I have no idea what this means.. what "other results"? Quote Link to comment https://forums.phpfreaks.com/topic/9710-need-help-with-an-order-by-query/#findComment-36248 Share on other sites More sharing options...
wardo Posted May 17, 2006 Author Share Posted May 17, 2006 [!--quoteo(post=374292:date=May 16 2006, 03:38 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ May 16 2006, 03:38 PM) [snapback]374292[/snapback][/div][div class=\'quotemain\'][!--quotec--]I have no idea what this means.. what "other results"?[/quote]I have a list of members in the database. Some are sponsors who apear at the top of the search results, the others are standard entries that appear after the sponors. It is the entries that appear after the sponsors that i want to be random.If I use the query suggested earlier the sponsors are random but the standard entries are not:SELECT * FROM members where (city like '%$city%' or country like '%$country%') and approved = '1' ORDER BY sponsor DESC, RAND() LIMIT $from, $max_results Quote Link to comment https://forums.phpfreaks.com/topic/9710-need-help-with-an-order-by-query/#findComment-36544 Share on other sites More sharing options...
fenway Posted May 17, 2006 Share Posted May 17, 2006 That doesn't seem right -- you'll get the sponsors in descending order first, and then all the remaining rows should be randomized. Post some sample records, and the output you get vs what you expect. Of course, you could always just do a UNION statement, but that seems like overkill. Quote Link to comment https://forums.phpfreaks.com/topic/9710-need-help-with-an-order-by-query/#findComment-36598 Share on other sites More sharing options...
wardo Posted May 17, 2006 Author Share Posted May 17, 2006 I just realised it actually works as intended, I didnt realise at first because there aren't that many records in the database.Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/9710-need-help-with-an-order-by-query/#findComment-36628 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.