0

how i can convert this query in codeigniter

select COUNT(*) 
from Retailers
where ID not In (select RetailerID from RetailerGroups)

i tried this

$this->db->where_not_in('ID',$this->db->query('select RetailerID from 
 RetailerGroups'));

 $query = $this->db->get('Retailers');

but it prints

Error Number: 42000

[Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near ')'.

SELECT * FROM Retailers WHERE ID NOT IN ()

Filename: D:\Published\faber\core\database\DB_driver.php

Line Number: 330

please help

1
  • More Detail: Message: Object of class CI_DB_sqlsrv_result could not be converted to string Commented Aug 29, 2013 at 12:54

2 Answers 2

1

I believe you can try use that syntax:

$this->db->select('count(*)')->from('Retailers');
$this->db->where('ID not in (select RetailerID from RetailerGroups)', NULL, FALSE);

The ,NULL,FALSE in the where() tells CodeIgniter not to escape the query.

Or you can just use JOIN with this query instead of subquery.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this once,

$idRs  = $this->db->select('RetailerID')->get('RetailerGroups')->result_array();
if( isset( $idRs ) && count( $idRs ) > 0 ){
    foreach( $idRs as $each ){
        $ids[]  = $each['RetailerID'];
    }
    echo "total :".$countRs    = $this->db->from('Retailers')->where_not_in('ID', $ids)->count_all_results();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.