![](/img/trans.png)
[英]SQL - Count number of rows with the same value without displaying the Count
[英]Selecting and displaying the count of rows with the same value in a table
基本上这是一个调查系统,我想从我的结果表中计算是和否
| q1 | q2 | q3 | q4 | q5 | q6 | q7 | q8 | q9 | q10 |
| Yes | Yes | No | No | No | Yes | No | Yes | Yes | No |
| No | Yes | No | Yes | Yes | Yes | No | No | Yes | No |
| Yes | Yes | No | No | No | Yes | No | Yes | Yes | No |
| No | Yes | No | Yes | Yes | Yes | No | No | Yes | No |
例如,我希望我的结果是q1 = 2否2是,q2 = 4是0否,依此类推,然后我希望将其显示在一个简单的表中,但是我仍然对查询一无所知
<body>
<form method="POST">
<table border="1">
<th>Answered Yes</th>
<th>Answered No</th>
</tr>
<?php
include("testdb.php");
$result= mysqli_query($con, "SELECT * FROM results");
if($result){
?>
</table>
</body>
只需使用布尔表达式:
select ((q1 = 'Yes') + (q2 = 'Yes') + . . . + (q10 = 'Yes')) as numYes
MySQL在数字上下文中将条件表达式视为数字,其中1表示true,0表示false。
在MySQL中(仅) true
为1
, false
为0
,因此对一个条件求和将计算它为true的次数:
select
sum((q1 = 'Yes') + (q2 = 'Yes') + ... etc) yeses,
sum((q1 = 'No') + (q2 = 'No') + ... etc) nod
from mytable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.