0 AND `cache_location`.`adm1`='&1' AND ISNULL(`cache_location`.`adm3`) AND `caches`.`status`=1", $rAdm1['adm1']); else sql("INSERT INTO &topLocationCaches (`cache_id`) SELECT `caches`.`cache_id` FROM `cache_location` INNER JOIN `caches` ON `caches`.`cache_id`=`cache_location`.`cache_id` LEFT JOIN `stat_caches` ON `caches`.`cache_id`=`stat_caches`.`cache_id` WHERE IFNULL(`stat_caches`.`toprating`,0)>0 AND `cache_location`.`adm1`='&1' AND `cache_location`.`adm3`='&2' AND `caches`.`status`=1", $rAdm1['adm1'], $rAdm3['adm3']); sql("INSERT INTO &topRatings (`cache_id`, `ratings`) SELECT `cache_rating`.`cache_id`, COUNT(`cache_rating`.`cache_id`) AS `ratings` FROM `cache_rating` INNER JOIN &topLocationCaches ON `cache_rating`.`cache_id`=&topLocationCaches.`cache_id` INNER JOIN `caches` ON `cache_rating`.`cache_id`=`caches`.`cache_id` WHERE `cache_rating`.`user_id`!=`caches`.`user_id` GROUP BY `cache_rating`.`cache_id`"); sql("INSERT INTO &topResult (`idx`, `cache_id`, `ratings`, `founds`) SELECT SQL_SMALL_RESULT (&topRatings.`ratings`+1)*(&topRatings.`ratings`+1)/(IFNULL(`stat_caches`.`found`, 0)/10+1)*100 AS `idx`, &topRatings.`cache_id`, &topRatings.`ratings`, IFNULL(`stat_caches`.`found`, 0) AS founds FROM &topRatings INNER JOIN `caches` ON &topRatings.`cache_id`=`caches`.`cache_id` LEFT JOIN `stat_caches` ON `stat_caches`.`cache_id`=`caches`.`cache_id` ORDER BY `idx` DESC LIMIT 15"); if (sql_value("SELECT COUNT(*) FROM &topResult", 0) > 10) { $min_idx = sql_value("SELECT `idx` FROM &topResult ORDER BY idx DESC LIMIT 9, 1", 0); sql("DELETE FROM &topResult WHERE `idx`<'&1'", $min_idx); } sql("INSERT INTO `rating_tops` (`cache_id`, `rating`) SELECT SQL_BUFFER_RESULT &topResult.`cache_id`, &topResult.`idx` AS `rating` FROM &topResult ORDER BY `rating` DESC"); } sql_free_result($rsAdm3); } sql_free_result($rsAdm1); sql_drop_temp_table('topLocationCaches'); sql_drop_temp_table('topRatings'); sql_drop_temp_table('topResult'); } } ?>