选择并显示表中具有相同值的行数 [英] Selecting and displaying the count of rows with the same value in a table

查看:157
本文介绍了选择并显示表中具有相同值的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上这是一个调查系统,我想从我的结果表中计算是和否,它有

Basically this is a survey system and I want to count the Yes's and No's from my results table which has

  | 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否等等
然后,我希望显示在一个简单的表,但我仍然无关的查询


For example I want my result to be q1 = 2 No's 2 Yes and q2 = 4 yes 0 No's and so on Then after that I was hoping to display it in a simple table but I'm still clueless about the query

<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>


推荐答案

真是 1 false 0 ,因此对条件求和有多少次是真的:

In MySQL (only) true is 1 and false is 0, so summing a condition counts how many times it's true:

select
    sum((q1 = 'Yes') + (q2 = 'Yes') + ... etc) yeses,
    sum((q1 = 'No') + (q2 = 'No') + ... etc) nod
from mytable

这篇关于选择并显示表中具有相同值的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆