简体   繁体   English

从一个表上存在但另一表上不存在的数据中选择

[英]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.

相关问题 SELECT表并从另一个表获取一些数据(如果存在) - SELECT table and get some data from another table if they exist 从一个表中选择 Laravel 5.1 中另一个表中不存在的所有记录 - Select all records from one table that do not exist in another table in Laravel 5.1 MySQL查询从表2中选择表1中不存在的数据 - mysql query to select data from table2 which does not exist in table1 SQL如何从另一个表中不存在的表中选择 - Sql how to select from a table that does not exist in another table 我如何检查一个表中是否存在会话ID,而另一表中是否存在该ID,则回显错误或成功代码 - how do i check if session ID exist from one table exist in another table and echo out error or success code PHP / SQL:如何从一个表中选择完全不同的表中不存在的值 - PHP/SQL: How to SELECT values from one table that do not exist in a completely different table 从一个表中仅选择另一个表中存在的记录 - Selecting only records from one table that exist in another table 使用laravel查询生成器并从一个表中查找另一个表中不存在的记录 - Query Builder and find records from one table which don't exist in another using laravel 如果列不存在,MySQL选择另一个表 - Mysql select another table if column not exist 选择另一个表中不存在的变量 - Select variables that don't exist in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM