计算此数据集的查询结果

时间:2012-01-02 02:14:34

标签: php mysql count

使用以下查询:

$result = mysql_query(
"SELECT Stats.champion
FROM Stats, Games
WHERE Stats.game_id = Games.game_id
AND Stats.win = 1
AND Games.game_mode = 'ODIN'");

我返回了以下数据集:

Ezreal 
Garen 
Pantheon 
Ahri 
Sion 
Rammus 
Nidalee 
Poppy 
Heimerdinger 
Graves 
KogMaw 
Gangplank 
Tristana 
Fizz 
Pantheon 
Pantheon 
Evelynn 
MasterYi 
Tryndamere 
Leona 
Vayne 
Malphite 
Graves 
Shaco 
Nidalee 
Graves 
Heimerdinger 
Gangplank 
JarvanIV 
Akali 

我的目标是计算每个名字的结果。我知道一个,例如“万神殿”,我能做到:

$result = mysql_query(
"SELECT Stats.champion
FROM Stats, Games
WHERE Stats.game_id = Games.game_id
AND Stats.champion = 'Pantheon'
AND Stats.win = 1
AND Games.game_mode = 'ODIN'");

$pantheonwins = mysql_num_rows($result);
echo $pantheonwins;

这会得到正确的信息!

Pantheon 
Pantheon 
Pantheon 
3

因此,由于这给了我正确的数据,这是获得每个名称的获胜次数的最有效方法,然后获得计算机百分比的总记录数?

3 个答案:

答案 0 :(得分:3)

尝试将您的查询更改为:

SELECT
    Stats.champion,
    count(Stats.champion) as myCount
FROM
    Stats, Games
WHERE 
    Stats.game_id = Games.game_id
    AND Stats.win = 1
    AND Games.game_mode = 'ODIN'
GROUP BY
    Stats.champion

现在,您应该在查询结果中获得计数和champion

答案 1 :(得分:2)

SELECT Stats.champion, COUNT(*) as aCount
FROM Stats, Games
WHERE Stats.game_id = Games.game_id
AND Stats.win = 1
AND Games.game_mode = 'ODIN'
GROUP BY Stats.champion

答案 2 :(得分:2)

怎么样:

SELECT Stats.champion
FROM Stats, Games
    WHERE Stats.game_id = Games.game_id
    AND Stats.champion = 'Pantheon'
    AND Stats.win = 1
    AND Games.game_mode = 'ODIN'
UNION
SELECT COUNT(Stats.champion) as champion
FROM Stats, Games
    WHERE Stats.game_id = Games.game_id
    AND Stats.champion = 'Pantheon'
    AND Stats.win = 1
    AND Games.game_mode = 'ODIN'

将返回

Champion 
=============
Pantheon 
Pantheon 
Pantheon 
3

但是如果你想得到这样的结果:

Champion       |        Result
==============================
Pantheon       |            3

然后你应该用这个:

SELECT STATS.champion as Champion, COUNT(GAMES.game_id) as `Result`
FROM Stats INNER JOIN Games ON  Stats.game_id = Games.game_id
WHERE Stats.win = 1 AND Games.game_mode = 'ODIN'
GROUP BY Champion