简体   繁体   English

用php计算Mysql表中的值数量

[英]Count the amount of values in a Mysql table with php

I made a school voting with 10 questions. 我对学校进行了10个问题的投票。 There you can check one teacher for every question (in total there are 80 teachers). 在那里,您可以为每个问题检查一位老师(总共有80位老师)。 When someone has voted for all questions, a new row in my mysql table will be inserted. 当有人对所有问题进行投票后,将在我的mysql表中插入新行。

My Mysql Table has 10 fields (for every question one(q1,q2,q3)). Mysql表有10个字段(每个问题一个(q1,q2,q3))。 The name of the teacher for one question will be an integer (every teachers has an id from 1 - 82). 一个问题的老师的名字将是一个整数(每个老师的ID从1到82)。

That is what the table looks like 那就是桌子的样子

Now I want to find out for which teacher is how often voted for each question. 现在,我想找出为每个问题投票的老师多长时间。 That should do a php script. 那应该做一个PHP脚本。 It should echo something like this: 它应该回显如下内容:

The result should be something like this 结果应该是这样的

How to do this? 这个怎么做?

This query will give number of times each teacher is voted for question q1. 该查询将给出每个老师对问题q1投票的次数。

$sql = "SELECT q1, COUNT(*) AS votes FROM table1 GROUP BY q1 ORDER BY votes";
$ref = $result->query($sql);
while($row = mysqli_fetch_assoc($ref))
{
  echo "$row[votes] times $row[q1]</br>";

}

You can do the same for other questions, just by changing the column name for question number 您只需更改问题编号的列名,即可对其他问题执行相同的操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM