【问题标题】:Counting how many MySQL fields in a row are filled (or empty) from two tables从两个表中计算一行中有多少 MySQL 字段被填充(或为空)
【发布时间】:2014-10-20 08:22:55
【问题描述】:

我需要组合一个方法,让我可以量化用户连续填写了多少字段。 并返回一个反映用户记录完成程度的值。这样的

as:
    User 1 = 100% complete
    User 2 = 80% complete
    User 3 = 60% complete

这里是同样的问题Counting how many MySQL fields in a row are filled (or empty)

但是如果我需要从 4 个带有 id 的表中查询呢?

我这样做是为了尝试

<?php
   $userId = $pro->id;
   $completeProfile = $db->prepare("SELECT * FROM hired_person_info WHERE id=?");
   $completeProfile = $db->prepare("SELECT * FROM hired_education_info WHERE id=?");
   $completeProfile = $db->prepare("SELECT * FROM hired_job_info WHERE id=?");
   $completeProfile = $db->prepare("SELECT * FROM hired_ts_info WHERE id=?");
   $completeProfile->bind_param('i', $userId);

if ($completeProfile->execute()) {   
$result = $completeProfile->get_result();
    while ($row = $result->fetch_row()) {
       $empty_count = 0;
       $count = count($row);
         for ($i = 0; $i < $count; $i++)
           if ($row[$i] === '' || $row[$i] === 'NULL')
           $empty_count++;

          $com = ((int)(100 * (1 - $empty_count / ($count - 1))));
           if ($com > 50) {
              echo "<span class='f_left width_autor textLeft GreenFont padding_8R'>".((int)(100 * (1 - $empty_count / ($count - 1))))."%</span>";
        } else {
             echo "".((int)(100 * (1 - $empty_count / ($count - 1))))."%";
     }
    }
  }
?>

但是当我尝试运行代码时,即使我尝试完成一些配置文件以使其达到 60%,它总是给出 8% 的完成率,如果我完成配置文件,它不会只给出 8% 或 100%

现在我怎样才能以正确的方式编写查询以使其按预期工作。 谢谢。

【问题讨论】:

  • 我不是 PDO 的专业人士,但正如我所见,您正在覆盖 $completeProfile 变量,因此当 $completeProfile-&gt;execute() 发生时,您只会得到最后的查询结果。
  • 所以你认为我应该更改每个查询的名称并再次执行每个查询,如 $completeProfile1-&gt;execute()$completeProfile2-&gt;execute() 我理解你的意思吗

标签: php mysql mysqli


【解决方案1】:

正如我在评论中提到的,我认为,因为您正在覆盖您的 $completeProfile 而没有执行它们,只有最后一个。尝试这样的事情(未测试,因为我没有您的表结构和您的自定义数据库处理程序函数)。

$userId = $pro->id;

$query1 = "SELECT * FROM hired_person_info WHERE id=?";
showUserPercentage($userId, $query1, $db);

$query2 = "SELECT * FROM hired_education_info WHERE id=?";
showUserPercentage($userId, $query2, $db);

$query3 = "SELECT * FROM hired_job_info WHERE id=?";
showUserPercentage($userId, $query3, $db);

$query4 = "SELECT * FROM hired_ts_info WHERE id=?";
showUserPercentage($userId, $query4, $db);

function showUserPercentage($userId, $query, $db) {
    $completeProfile = $db->prepare();
    $completeProfile->bind_param('i', $userId);
    if ($completeProfile->execute()) {
        $result = $completeProfile->get_result();
        while ($row = $result->fetch_row()) {
            $empty_count = 0;
            $count = count($row);
            for ($i = 0; $i < $count; $i++) {
                if ($row[$i] === '' || $row[$i] === 'NULL') {
                    $empty_count++;
                }
            }
            $com = ((int) (100 * (1 - $empty_count / ($count - 1))));
            if ($com > 50) {
                echo "<span class='f_left width_autor textLeft GreenFont padding_8R'>" . ((int) (100 * (1 - $empty_count / ($count - 1)))) . "%</span>";
            } else {
                echo "" . ((int) (100 * (1 - $empty_count / ($count - 1)))) . "%";
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    @Mikky 试试这个

    <?php
       $personProfile = $db->prepare("SELECT * FROM hired_person_info AS t1
                                    JOIN hired_education_info AS t2
                                    ON t1.id=t2.uid
                                    WHERE t1.id=? GROUP BY t1.id ");
    
    if ($completeProfile->execute()) {   
    $result = $completeProfile->get_result();
        while ($row = $result->fetch_row()) {
           $empty_count = 0;
           $count = count($row);
             for ($i = 0; $i < $count; $i++)
               if ($row[$i] === '' || $row[$i] === 'NULL')
               $empty_count++;
    
              $com = ((int)(100 * (1 - $empty_count / ($count - 1))));
               if ($com > 50) {
                  echo "<span class='f_left width_autor textLeft GreenFont padding_8R'>".((int)(100 * (1 - $empty_count / ($count - 1))))."%</span>";
            } else {
                 echo "".((int)(100 * (1 - $empty_count / ($count - 1))))."%";
         }
        }
      }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2011-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多