Jump to content

Need help with an order by query


wardo

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.
Link to comment
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.
Link to comment
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"?
Link to comment
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
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.