Travus Gonzalez
Travus Gonzalez

Reputation: 89

Randomizing a specific group mysql

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 show above regular members but randomized and not in alphabetical order. I have tried RAND() but it randomizes all my members, please help.

Upvotes: 0

Views: 41

Answers (2)

Michael Fredrickson
Michael Fredrickson

Reputation: 37388

SELECT * 
FROM members 
WHERE category1="Photographers" 
ORDER BY 
    premium DESC, 
    featured DESC, 
    CASE WHEN featured = 1 THEN RAND() ELSE company END ASC

Upvotes: 2

Matt Busche
Matt Busche

Reputation: 14333

You can use a CASE in an ORDER BY something like

SELECT * 
FROM members 
WHERE category1="Photographers" 
ORDER BY CASE WHEN Featured = 1 THEN 0 ELSE 1 END, Rand()

Upvotes: 1

Related Questions