1,105,375 Community Members

Randomizing a specific group

Member Avatar
Travus
Light Poster
32 posts since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

So I have this line of code:

$results = mysql_query('SELECT * FROM members WHERE category1="Photographers" ORDER BY premium DESC, featured DESC, company ASC');

Everything works great, Premium members show up above featured. Featured above regular members, and they are all in alphabetical order. However I want to have featured members randomized and not in alphabetical order. I have tried RAND() but it randomizes all my members, please help.

Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

Use a UNION. First select your premiums, second the featured randomized.

Member Avatar
Travus
Light Poster
32 posts since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Could you give me a code example? I keep getting the error "Incorrect usage of UNION and ORDER BY". Obviously I am using it wrong but I am not sure how.

Member Avatar
shahidpk
Newbie Poster
3 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Just cut the "featured DESC" from your query and check the result.

Member Avatar
Travus
Light Poster
32 posts since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Cutting out "featured DESC" makes the featured members get mixed with regular members and that is not what I want. What I want is to have something like this:

Premium Member
Featured Member Rand()
Normal Member Alphabetical

Thanks for your reply.

Member Avatar
Travus
Light Poster
32 posts since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
1
 

I found a solution on the web. Thanks for everyones help!

SELECT * 
FROM members 
WHERE category1="Photographers" 
ORDER BY 
    premium DESC, 
    featured DESC, 
    CASE WHEN featured = 1 THEN RAND() ELSE company END ASC
Question Answered as of 1 Year Ago by pritaeas and shahidpk
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article