0

私が運営するウェブサイトでは、ユーザーに私たちのウェブサイトを評価してもらいます。データベースにデータを保存します。8つの異なる答えがあります。各値の投票率を取得するにはどうすればよいですか。

私のテーブルはこんな感じ

id-----投票
1-------p1

2------p1

3------p2

4------p3

5------p4

6------p3

...

4

3 に答える 3

3

これを使用できます:

select vote, 
       count(*) / (select count(*) from your_table) * 100 as percentage
from your_table
group by vote
于 2012-10-11T15:27:04.053 に答える
1

テーブルpollsがあり、voteidフィールドには、その 1 票に対して発行された優先順位が示されています。

あなたの最善の選択肢は、単に実行することだと思います

SELECT voteid, count(voteid) AS votes FROM polls GROUP BY voteid;

次に、PHP でデータを復元します。

$votes = array();
$total = 0;
while($vote = mysql_fetch_array($exec)) // or PDO, or ...
{
    list($voteid, $num) = $vote;
    $votes[$voteid] = $num;
    $total += $num;
}
// Convert absolute number to percentages
$percents = array();
foreach($votes as $vote => $count)
    $percents[$vote] = number_format(($count*100.0)/$total, 2);
于 2012-10-11T15:34:09.007 に答える
0

It's a two folds way IMO, one gets the data, the other shows it but here goes:

SELECT vote, count(vote)
FROM votes
GROUP BY vote WITH ROLLUP

This will give you all your vote counts (Doesn't include the answers without votes, you have to infer them or change your model) and the last line that has a NULL vote will be your total votes all at once.

So you place those in memory as you should always do, pass this on to your display method (view, template, wathever you use) and this one will:

echo $voteCount / $totalVotes * 100;

Cheers

于 2012-10-11T15:29:25.517 に答える