Jump to content


Photo

Need help with an order by query


  • Please log in to reply
6 replies to this topic

#1 wardo

wardo
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 15 May 2006 - 01:45 PM

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.
www.realdesignnetwork.com

#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 15 May 2006 - 04:24 PM

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] * FROM members where (city like '%$city%' or country like '%$country%') and approved = '1' ORDER BY sponsor DESC, RAND() LIMIT $from, $max_results [!--sql2--][/div][!--sql3--]

This will create a random sub-order within the sponsor ordering.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#3 wardo

wardo
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 16 May 2006 - 08:20 AM

Thanks,

Thats almost it but not quite.

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

Any other ideas?
www.realdesignnetwork.com

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 16 May 2006 - 02:38 PM

[!--quoteo(post=374216:date=May 16 2006, 04:20 AM:name=wardo)--][div class=\'quotetop\']QUOTE(wardo @ May 16 2006, 04:20 AM) View Post[/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"?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 wardo

wardo
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 17 May 2006 - 07:55 AM

[!--quoteo(post=374292:date=May 16 2006, 03:38 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ May 16 2006, 03:38 PM) View Post[/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
www.realdesignnetwork.com

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 17 May 2006 - 02:11 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 wardo

wardo
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 17 May 2006 - 03:12 PM

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.
www.realdesignnetwork.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users