简体   繁体   中英

How to implement “WHERE NOT IN” query into Codeigniter Active records?

I have a MySQL query which is this:

SELECT * FROM tbl_post WHERE tbl_post.post_id NOT IN 
(SELECT tbl_readsave.post_id FROM tbl_readsave)

I want to convert it into Codeigniter Active records, so I used the following code segment:

    $this->db->select('tbl_readsave.post_id');
    $queryReadSave = $this->db->get('readsave');
    $this->db->where_not_in('post_id', $queryReadSave->result_array());
    $queryNewPost = $this->db->get('readsave');
    if($queryNewPost->num_rows()>0)
    {
        return $queryNewPost->result_array();
    }
    else
        return false;

However, the code throws me an error, which is like the following:-

Error Number: 1054

Unknown column 'Array' in 'where clause'

SELECT * FROM (`tbl_readsave`) WHERE `post_id` NOT IN (Array)

Filename: /var/www/html/teamF/tharjumal/models/webservice_model.php

Line Number: 28

How can I convert the above stated query into Codeigniter Active Records format?

$queryReadSave->result_array() returns an array of arrays. You can't use that in where_not_in .

You need to loop over that and create a flat array of the IDs you (don't) want.

$this->db->select('post_id');
$queryReadSave = $this->db->get('readsave');

$postIDs = $queryReadSave->result_array();

$this->db->where_not_in('post_id', array_column($postIDs, 'post_id'));
$queryNewPost = $this->db->get('post');

array_column() only exists in PHP 5.5+. If you are on a lower version, you'll need to do something like this:

$this->db->select('post_id');
$queryReadSave = $this->db->get('readsave');

$postIDs = array_map(function($a){
    return $a['post_id'];
}, $queryReadSave->result_array());

$this->db->where_not_in('post_id', $postIDs);
$queryNewPost = $this->db->get('post');

PS Your second table is called post , right? You'll need to update the query to use the right table.

Update: Your original query uses a subquery which is not natively supported by CodeIgniter. If you want to try this all as one query, you can use the Subquery library I created ( https://github.com/NTICompass/CodeIgniter-Subqueries ).

$this->db->select('*');
$this->db->from('post');

$sub = $this->subquery->start_subquery('where_in');
$sub->select('post_id')->from('readsave');

$this->subquery->end_subquery('post_id', FALSE);
$queryNewPost = $this->db->get();

try below code:

$read_post_id = [];

$queryReadSave = $this->db->select('post_id')->get('tbl_readsave')->result_array();

if(count($queryReadSave) > 0){

     foreach($queryReadSave as $row){

        $read_post_id[] = $row['post_id']; // add each post id to the array
     }

}

$this->db->select('*');

if(!empty($read_post_id)) $this->db->where_not_in('post_id',$read_post_id);

$post = $this->db->get('tbl_post');

print_r($post->result_array());

exit;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM