Sorting in numerical order, and then randomly

From: Date: Sun, 17 Nov 2002 20:55:20 +0000
Subject: Sorting in numerical order, and then randomly
Groups: php.db 
Request: Send a blank email to php-db+get-23429@lists.php.net to get a copy of this message
I am using MySQL to store ad information in the following table: CREATE TABLE IF NOT EXISTS ads_value ( img_link varchar(50), text text, service varchar(50) default NULL, title varchar(50) default NULL, priority int(2) default '0', status enum('current', 'old'), ID int(3) NOT NULL auto_increment, PRIMARY KEY (ID) ) TYPE=MyISAM; Ads for which advertisers pay more will have a higher priority - i.e. 1, 2, 3, etc. Everything else will have a priority of 0. When the page loads, I want to first display any ads that have a priority higher than 0 to be displayed in order, and then the remaining ads with a priority if 0 to be displayed in random order. They have to be displayed in a different order each time, so that each ad has the same chance of being displayed in a particular spot as any other ad. The only spots a random ad cannot be in is one taken by a higher paying ad. I hope this is clear. Is it possible to do this with one query? Or would I have to use 2 different queries: select * from ads_value where status = 'current' and priority > 0 order by priority asc and then select * from ads_value where status = 'current' and priority = 0 order by RAND() TIA, -Lisi

« previous php.db (#23429) next »