[!--quoteo(post=347468:date=Feb 19 2006, 01:43 PM:name=shoz)--][div class=\'quotetop\']QUOTE(shoz @ Feb 19 2006, 01:43 PM) [snapback]347468[/snapback][/div][div class=\'quotemain\'][!--quotec--] I WOULD NOT suggest doing it this way, but using MYSQL 4.1 I was able to get the desired result using one query with the following. To create table `users` [code] CREATE TABLE `users` ( `name_id` int(11) NOT NULL auto_increment, `name` varchar(60) default NULL, `rank` int(11) default NULL, PRIMARY KEY (`name_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO `users` VALUES (1,'john',0),(2,'steve',0),(3,'smith',0),(4,'fred',0); [/code] The query we want to rank by [code] SELECT * FROM users ORDER BY name DESC [/code] query to update the rank [code] UPDATE users AS n1 INNER JOIN ( SELECT @cc:=1, sn2.name_id, sn2.name, ( SELECT (SUM(IF(sn1.name_id=sn2.name_id, @cc:=0, @cc))+1) FROM ( SELECT * FROM users ORDER BY name DESC ) AS sn1 ) AS rank FROM users AS sn2 ORDER BY name DESC ) AS n2 ON n1.name_id=n2.name_id SET n1.rank=n2.rank; [/code] Using the temporary table option suggested by fenway you can create the temporary table with an auto_increment column and create the update query based on that. [code] CREATE TEMPORARY TABLE users_tmp (rank INT AUTO_INCREMENT, name_id INT, PRIMARY KEY(rank)); INSERT INTO users_tmp SELECT '', id FROM users ORDER BY name DESC; UPDATE users n1 INNER JOIN users_tmp n2 ON n1.id=n2.name_id SET n1.rank=n2.rank; [/code] You should [a href=\"http://dev.mysql.com/doc/refman/4.1/en/lock-tables.html\" target=\"_blank\"]lock[/a] the table before running the queries or find some other way to ensure that the table doesn't change during the queries. If you'd like to be able to continue "Selecting" from the table during the update, consider using [a href=\"http://www.innodb.com/index.php\" target=\"_blank\"]InnoDB[/a] tables. Using php you can create the update using something similar to the following [code] $query = 'SELECT * FROM users ORDER BY name DESC '; $update_query = 'UPDATE users SET rank = CASE name_id '; for ($i = 1;$row = mysql_etc($result); $i++) { $update_query .= "WHEN {$row['name_id']} THEN $i "; } $update_query .= 'END '; mysql_query($update_query); [/code] [a href=\"http://dev.mysql.com/doc/refman/4.1/en/control-flow-functions.html\" target=\"_blank\"]Control Flow Functions[/a] Again, lock the table when doing the queries. [/quote] why would you NOT want to do this?