dragon19720808 2014-05-02 10:59
浏览 34

mysql计算已完成列的行数,还计算不完整列的行数?

i am using this script to count how many rows exist in my table that have completed columns like so

<?php include 'config.php';

    $q = "SELECT * FROM supplier_session WHERE form1_completed = 'Yes' AND form2_completed = 'Yes' AND form3_completed = 'Yes' AND form4_completed = 'Yes'" or die (mysql_error());
    $result = mysql_query($q);
    $count = mysql_num_rows($result);
?>

<?php echo "(<font color=red>$count</font>)"; ?>

this script says that if 'form1_completed, form2_completed, form3_completeed and form4_compelted all = 'yes' then to count the number of rows.

Now what i want to do, is have a seperate count that shows the number of rows that are incomplete, so baring in mind that some rows may only have 'form1_completed' as 'yes' and 'form2_completed' as 'no'. i need to basically count any rows that do not have all four columns completed or set as yes, so 'form1_complted', 'form2_completed', 'form3_completed', 'form4_completed' if these are not all yes then to count the rows

could someone please show me how i can do this? thanks

  • 写回答

2条回答 默认 最新

  • dongre6073 2014-05-02 11:19
    关注

    Try using conditional aggregation:

    SELECT sum(form1_completed = 'Yes' AND form2_completed = 'Yes' AND
               form3_completed = 'Yes' AND form4_completed = 'Yes'
              ) as NumAllCompleted,
           sum(not (form1_completed = 'Yes' AND form2_completed = 'Yes' AND
                    form3_completed = 'Yes' AND form4_completed = 'Yes'
                   )
              ) as NumIncomplete
    FROM supplier_session;
    

    This assumes that the completed flags never take on NULL values.

    Note: it is usually a bad idea to store "arrays" over values in multiple columns. You should have a junction/association table for each form. This would have one row for user/form combination, along with the status of that form. And it might have other information such as the date/time the form was completed.

    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答