0

我如何在 codeigniter 中转换这个查询

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

我试过这个

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

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

但它打印

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

请帮忙

4

2 回答 2

1

我相信您可以尝试使用该语法:

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

where() 中的 ,​​NULL,FALSE 告诉 CodeIgniter 不要逃避查询。

或者您可以只使用 JOIN 与此查询而不是子查询。

于 2013-08-29T13:34:36.080 回答
0

试试这个,

$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();
}
于 2013-08-29T13:45:37.710 回答