[英]select from data which exist on one table but do not exist on another table
using code igniter!使用代码点火器! I have two tables student and student_class with foreign key student_id, i want to pick data which exists on student table but not found on class_student我有两个表student和student_class,外键是student_id,我想选择存在于student表但在class_student上找不到的数据
here is my sql这是我的 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();
}
it does not work out!!它不起作用!! can i get help..我能得到帮助吗..
Try like this..试试这样..
First find all student ids that are matched with student_class table.Then use $this->db->where_not_in
to get your required result.首先找到所有与student_class 表匹配的student 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();
}
Hope it will works.希望它会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.