如何在CodeIgniter的查询生成器中编写包含联接的查询?

时间:2017-04-08 04:05:35

标签: php codeigniter

我想写的查询是

SELECT Equipment.Name, Equipment.EquipmentTag, LoanedOut.StudentNumber, LoanedOut.DueDate 
FROM Equipment, LoanedOut 
WHERE Equipment.EquipmentRecordID = LoanedOut.EquipmentRecordID AND LoanedOut.StudentNumber = 040828055

我无法弄清楚如何使用codeigniter的查询构建器来做到这一点,我到目前为止最好的是

$this->db->select('Equipment.Name, Equipment.EquipmentTag, LoanedOut.StudentNumber, LoanedOut.DueDate');
$this->db->from('Equipment e, LoanedOut l');
$this->db->join('l', 'e.EquipmentRecordID = l.EquipmentRecordID')->join('l', 'l.StudentNumber', $studentNumber);

1 个答案:

答案 0 :(得分:0)

这会给你你想要的东西//

 $sql = 'SELECT e.Name,e.EquipmentTag,l.StudentNumber,l.DueDate FROM Equipment e LEFT JOIN LoanedOut l ON e.EquipmentRecordID = l.EquipmentRecordID WHERE l.StudentNumber = "040828055"'
$query = $this->db->query($sql);