使用代码点火器!我有两个表student和student_class与外键student_id,我想选择存在于学生表但在class_student上找不到的数据
这是我的sql
function student_class(){
$this->db->SELECT ('student.student_id, student.firstname, student.middlename, student.lastname');
$this->db->FROM('student');
$this->db->WHERE('student.status',0);
$this->db->JOIN('student_class', 'student_class.student_id=student_class.student_id', 'left');
$this->db->where_not_in('student_class.student_id');
$query =$this->db->get();
return $query->result_array();
}
它不起作用!! 我可以得到帮助..
答案 0 :(得分:0)
试试这样..
首先查找与student_class表匹配的所有学生ID。然后使用$this->db->where_not_in
获取所需的结果。
function student_class(){
$this->db->select ('student.student_id');
$this->db->from ('student');
$this->db->join('student_class', 'student.student_id=student_class.student_id', 'left');
$this->db->where('student.status',0);
$data = $this->db->result_array();//array of matched id's
$this->db->select('student_id,firstname, middlename,lastname');
$this->db->from('student');
$this->db->where_not_in($data);
$query = $this->db->get();
return $query->result_array();
}
希望它能奏效。