We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,303 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Randomizing a specific group

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.

3
Contributors
5
Replies
22 Hours
Discussion Span
3 Months Ago
Last Updated
28
Views
Question
Answered
Travus
Light Poster
32 posts since Jul 2012
Reputation Points: 0
Solved Threads: 4
Skill Endorsements: 0

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

pritaeas
Posting Prodigy
Moderator
9,534 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,494
Skill Endorsements: 98

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.

Travus
Light Poster
32 posts since Jul 2012
Reputation Points: 0
Solved Threads: 4
Skill Endorsements: 0

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

shahidpk
Newbie Poster
3 posts since Oct 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 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.

Travus
Light Poster
32 posts since Jul 2012
Reputation Points: 0
Solved Threads: 4
Skill Endorsements: 0

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
Travus
Light Poster
32 posts since Jul 2012
Reputation Points: 0
Solved Threads: 4
Skill Endorsements: 0
Question Answered as of 3 Months Ago by pritaeas and shahidpk

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0802 seconds using 2.7MB