php - 如果使用 codeigniter 某些列为空,则查询不显示结果

标签 php mysql codeigniter

大家好,我正在尝试使用 mysql 和 codeigniter 从两个不同的表中获取结果,实际上我能够做到,问题是一个表 (empleados) 包含名称、姓氏等数据。(看图片)和另一个表(cuentas)有空列,因为我稍后会根据结果插入它们,问题是如果 cuentas 没有存储任何数据,它不会显示结果其他表(姓名、姓氏等),所以即使其他表没有存储数据,我也想查看结果。

这是 Controller :

function search2()
{
  if ($_POST) {
      $Interno=$_POST['Interno'];
  }else{
    $Interno = '';
  }
  $this->db->select('empleados.Interno, empleados.Curp, empleados.Nombre, empleados.A_Paterno, empleados.A_Materno, cuentas.Clabe, cuentas.Banco, cuentas.Observaciones, cuentas.Status');
$this->db->from('empleados');
$this->db->join('cuentas','cuentas.Interno = empleados.Interno');
$this->db->where('empleados.Interno', $Interno);
$this->db->where('cuentas.Status !=', 'I');
$q = $this->db->get();
$data = array();
$data['records'] = $q->result_array();
$this ->load -> view('main/indice', $data); 


}

这是 View :

 <?php
    foreach ($records as $row) {
            echo"<br></br>";
            echo "<label class='control-label'>Interno</label>";
            echo "<input type='text' id='Interno'  name ='Interno' class='form-control Input col-sm-3' placeholder='Interno' value='".$row['Interno']."'>";
            echo "<label class='control-label'>CURP</label>";
            echo "<input type='text' id='curp' name ='curp' class='form-control Input col-sm-3' placeholder='Curp' value='".$row['Curp']."'>";
            echo "<label class='control-label'>nombre</label>";
            echo "<input type='text' id='nombre' name ='nombre' class='form-control Input col-sm-3' placeholder='Nombre' value='".$row['Nombre']."'>";
            echo "<label class='control-label'>Apellido Paterno</label>";
            echo "<input type='text' id='A_Paterno' name ='A_Paterno' class='form-control Input col-sm-2' placeholder='Apellido Paterno' value='".$row['A_Paterno']."'>";
            echo "<label class='control-label'>Apellido Materno</label>";
            echo "<input type='text' id='A_Materno' name ='A_Materno' class='form-control Input col-sm-1' placeholder='Apellido Materno' value='".$row['A_Materno']."'>"; 

               echo "<label class='control-label'>Clabe</label>";
            echo "<input type='text' id='Clabe' name ='Clabe' class='form-control Input col-sm-3' placeholder='' value='".$row['Clabe']."'>";
            echo "<label class='control-label'>Banco</label>";
            echo "<input type='text' id='Banco' name ='Banco' class='form-control Input col-sm-2' placeholder='' value='".$row['Banco']."'>";
            echo "<label class='control-label'>Observaciones</label>";
            echo "<input type='text' id='Observaciones' name ='Observaciones' class='form-control Input col-sm-1' placeholder='' value='".$row['Observaciones']."'>"; 
    }
  ?>

这是表“cuentas”没有任何信息但表“empleados”有的页面。 enter image description here

这是表“cuentas”有数据时的页面 enter image description here

我再次希望看到第一个表的结果,即使第二个表的字段为空。

最佳答案

你需要一个左连接而不是内连接,同时将你的cuentas.Status过滤器移动到连接部分,这样做你会得到empleados记录如果没有关联在 cuentas 表中找到的行

$this->db->select('empleados.Interno, empleados.Curp, empleados.Nombre, empleados.A_Paterno, empleados.A_Materno, cuentas.Clabe, cuentas.Banco, cuentas.Observaciones, cuentas.Status');
$this->db->from('empleados');
$this->db->join('cuentas',"cuentas.Interno = empleados.Interno AND cuentas.Status !='I'", 'left');
$this->db->where('empleados.Interno', $Interno);
$q = $this->db->get();

关于php - 如果使用 codeigniter 某些列为空,则查询不显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50974405/

相关文章:

php - 是否可以使用我的数据库结构使用 Laravel 或 Sql 执行此查询?

javascript - 使用AJAX onclick函数返回PHP Mysql查询

php - 将参数传递给 Controller ​​总是得到 "Page Not Found"- CodeIgniter

php - 错误 : This feature is not available for the database you are using

php - 有没有办法从 PHP 8+ 函数获取命名参数

javascript - 带有 li & ul 的多级下拉列表,带有 PHP 数组循环,带有 CSS 和 jQuery

java - 如何从结果集中获取最小计数、最大计数和总结果集计数

Codeigniter 使用 AND 和 OR 条件选择查询

php - 使用 SQL 更新 MySQL 表中的列的数组到列行

php - PHP 中是否有一系列的扩展/模块加载?