Jump to content

Archived

This topic is now archived and is closed to further replies.

wardo

Need help with an order by query

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thanks,

Thats almost it but not quite.

I need the other results to be random as well as the sponsors.

Any other ideas?

Share this post


Link to post
Share on other sites
[!--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"?

Share this post


Link to post
Share on other sites
[!--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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.