<?php
$db_host="localhost";
$db_user="root";
$db_password="pass";
$db_name = "database";
$table = "table";
mysql_connect( $db_host, $db_user, $db_password );
mysql_select_db( $db_name );
mysql_query( "SET NAMES 'utf8'" );
$apps = mysql_query( "SELECT `author_id` FROM `".$table."` ORDER BY `points` DESC" );
$apps_top = array();
$app_num = 0;
while($current_app = mysql_fetch_array($apps))
{
$apps_top[$app_num] = $current_app['app_id'];
$app_num ++;
}
$temporary_table = "CREATE TEMPORARY TABLE IF NOT EXISTS `temporary_top` (";
$temporary_table .= "`id` int(15) auto_increment,";
$temporary_table .= "`author_id` int(15) default NULL,";
$temporary_table .= "`pos` int(25) default '0',";
$temporary_table .= "PRIMARY KEY (`id`),";
$temporary_table .= "UNIQUE KEY `author_id` (`author_id`)";
$temporary_table .= ") ENGINE=MyISAM DEFAULT CHARSET=utf8; ";
$temporary_table .= "INSERT INTO `temporary_top` (`author_id`, `pos`) VALUES ";
for($i = 0; $i < count($apps_top); $i ++)
{
if($i != count($apps_top) - 1)
{
$temporary_table .= "('" . $apps_top[$i] . "','" . ($i + 1) . "'),";
}
else
{
$temporary_table .= "('" . $apps_top[$i] . "','" . ($i + 1) . "'); ";
}
}
$query = "UPDATE ".$table." JOIN `temporary_top` SET ".$table.".`pos` = temporary_top.`pos` WHERE ".$table.".`author_id` = temporary_top.`author_id`;";
//Тест
echo $temporary_table . "<br>";
echo $query . "<br><br>";
mysql_query($temporary_table.$query) or die ( "Ошибка: ".mysql_error());
?>