我有两个数据库表,一个有 165 个条目。对于其中的每一个,我都要浏览 100 万个条目的表格,看看这 165 个条目中的每一个被提到了多少次。
“odds_provider”有 165 个条目,“bettingoffer”有 100 万个条目。
$SQL = "SELECT
odds_provider.id AS id,
odds_provider.name AS name,
COUNT(bettingoffer.odds_providerFK) AS betcount
FROM odds_provider
INNER JOIN
bettingoffer
ON bettingoffer.odds_providerFK = odds_provider.id
WHERE
odds_provider.active = 'yes'
GROUP BY
odds_provider.id,
odds_provider.name
ORDER BY betcount DESC";
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)) {
echo $db_field['id'] , " " , $db_field['name'] , " " , $db_field['betcount'] , "</BR>";
}
它的作用是预期的,但它需要永远。有没有更快的方法?
最佳答案
这个应该更快,尤其是在没有很多非事件 ID 的情况下。
SELECT id, name, betcount
FROM (
SELECT
odds_providerFK as id,
COUNT(*) as betcount
FROM bettingoffer
WHERE active = 'yes'
GROUP BY odds_providerFK
ORDER BY betcount DESC) as counts
USING (id);
关于php - 计算大型数据库的结果数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8788361/