回应每个记录总投票总票数的百分比

时间:2014-03-12 15:08:27

标签: php mysql

我需要回应每张唱片总投票总票数的百分比。我知道这个公式,但无法用PHP来解决这个问题。

<?php

$totalvotes = "SELECT SUM(votes) AS total FROM voting";


$totalvotesresults = mysql_query( $totalvotes )
or die( "Could not get total votes " .mysql_error() );



$data = mysql_fetch_object( $totalvotesresults );
echo "<div>Total number of votes is ". $data->total ."</div>\n";


?>

<?php

$artistname = "SELECT * FROM voting";

$artistnameresults = mysql_query( $artistname )
or die( "Could not get video games " .mysql_error() );

for( $i = 0; $i < mysql_numrows( $artistnameresults ); $i++ ) {



$data = mysql_fetch_array( $artistnameresults );
echo "<div>". $data['artist'] ." has " . $data['votes'] . "  votes ( % of total )</div>\n";


} // ends for loop

?>

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你可以在一个查询中完成。

如果您的表格每个艺术家只包含一行

SELECT artist, votes, total, votes / total * 100 percent
  FROM voting v CROSS JOIN
(
  SELECT SUM(votes) total 
    FROM voting
) t
 ORDER BY artist;

如果您的表格允许每位艺术家多行

SELECT artist, SUM(votes) votes, total, SUM(votes) / total * 100 percent
  FROM voting v CROSS JOIN
(
  SELECT SUM(votes) total 
    FROM voting
) t 
 GROUP BY artist;

两个查询的示例输出:

------------------------------------------
| artist       | votes | total | percent |
------------------------------------------
| Arcangel     | 17    | 63    | 26.9841 |
| Daddy Yankee | 4     | 63    | 6.3492  |
| Farruko      | 14    | 63    | 22.2222 |
| J Alvarez    | 13    | 63    | 20.6349 |
| Jory         | 15    | 63    | 23.8095 |
------------------------------------------

这是 SQLFiddle 演示


然后您的PHP代码可能看起来

$sql = "SELECT artist, votes, total, votes / total * 100 percent
          FROM voting v CROSS JOIN
        (
          SELECT SUM(votes) total 
            FROM voting
        ) t
         ORDER BY artist";

$result = mysql_query($sql);
if(!$result) {
    die(mysql_error()); // TODO: better error handling
}

// grab the first row to spit out the total
if ($row = mysql_fetch_assoc($result)) {
    echo "<div>Total number of votes is {$row['total']} </div><hr>";
    displayArtist($row);
}
// display other rows
while($row = mysql_fetch_assoc($result)) {
    displayRow($row);
}

function displayRow($row) {
    echo "<div>{$row['artist']} has {$row['votes']} ({$row['percent']}% of total)</div>";    
}

或者您可以简单地执行此操作

$sql = "SELECT artist, votes FROM voting ORDER BY artist";
$result = mysql_query($sql);
if (!$result) {
    die(mysql_error()); // TODO: better error handling
}

$total = 0;
$rows = array();
while($row = mysql_fetch_assoc($result)) {
    $rows[] = $row;
    $total += $row['votes'];
}
mysql_close();

echo "<div>Total number of votes is $total </div><hr>";
foreach ($rows as $row) {
    displayRow($row, $total);
}

function displayRow($row, $total) {
    $percent = round($row['votes'] / $total * 100, 4);
    echo "<div>{$row['artist']} has {$row['votes']} ($percent% of total)</div>";    
}

输出:

Total number of votes is 63
------------------------------------
Arcangel has 17 (26.9841% of total)
Daddy Yankee has 4 (6.3492% of total)
Farruko has 14 (22.2222% of total)
J Alvarez has 13 (20.6349% of total)
Jory has 15 (23.8095% of total)

旁注:了解并使用PDOmysqli扩展名的预准备语句。 {@ 1}}已被弃用。