计算特定行

时间:2015-10-16 17:58:05

标签: php

我正在使用bootstrap进度条显示统计信息。

我想知道在总行数中有多少得分为10分。

$q="SELECT * FROM users";
$r = mysqli_query($con,$q);
$total = mysqli_num_rows($r);

Name   email          score time
Hello  abc@gmail.com  10   15
Hello  abc@gmail.com  58    10
Test   def@gmail.com  10   12 
Stack  xyz@gmail.com  90    20
Test   def@gmail.com  50    40

3 个答案:

答案 0 :(得分:0)

$ q =" SELECT COUNT(Name)FROM users WHERE score =' 10' &#34 ;;

答案 1 :(得分:0)

您可以使用此简单查询来完成此操作: -

SELECT COUNT(name), score 
FROM users
GROUP BY score

这将得到每个人得分(10,9,8)的总和。 如果您只想要总值10。请改用它。

SELECT COUNT(name), score 
FROM users
WHERE score=10

答案 2 :(得分:0)

这将循环遍历$ total数组,并将所有小于10的值分配到一个数组中,然后循环遍历该数组,以打印出每个少于10的总数。

    $total = [
        0 => [
            'name' => 'Hello',
            'email' => 'abc@gmail.com',
            'score' => 10,
            'time' => 15
        ],
        1 => [
            'name' => 'Hello',
            'email' => 'abc@gmail.com',
            'score' => 58,
            'time' => 10
        ],
        2 => [
            'name' => 'Test',
            'email' => 'def@gmail.com',
            'score' => 10,
            'time' => 12
        ],
        3 => [
            'name' => 'Stack',
            'email' => 'xyz@gmail.com',
            'score' => 90,
            'time' => 20
        ],
        4 => [
            'name' => 'Test',
            'email' => 'def@gmail.com',
            'score' => 50,
            'time' => 40
        ],
    ];

    $sizeOfArray = count($total);
    $arrayOfScoresLessThanTen = [];
    foreach($total as $value) {
        if($value['score'] <= 10) {
            if(isset($arrayOfScoresLessThanTen[$value['score']])) {
                $arrayOfScoresLessThanTen[$value['score']] ++;
            }
            else {
                $arrayOfScoresLessThanTen[$value['score']] = 1;
            }
        }
    }

    foreach($arrayOfScoresLessThanTen as $key => $scoresLessThanTen) {
        echo 'Score: '. $key . ' number scored out of total: '. $scoresLessThanTen . '/'.$sizeOfArray;
    }

这已经过测试,可以使用我创建的数组$ total来匹配表中的数组。我添加了我的$ total数组,以便您可以自己测试它。