计算一行中有多少个MySQL字段从两个表中填充(或清空)

时间:2014-10-20 08:22:55

标签: php mysql mysqli

我需要整理一个方法,以便我可以量化用户填充的行数。 并返回一个反映用户记录完成程度的值。这样的

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%简档

现在我如何以正确的方式编写查询以使其按预期工作。 感谢。

2 个答案:

答案 0 :(得分:1)

@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))))."%";
     }
    }
  }
?>

答案 1 :(得分:0)

正如我在评论中提到的,我认为,因为你覆盖了你的$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)))) . "%";
            }
        }
    }
}