我如何在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
请帮助
答案 0 :(得分:1)
我相信您可以尝试使用该语法:
$this->db->select('count(*)')->from('Retailers');
$this->db->where('ID not in (select RetailerID from RetailerGroups)', NULL, FALSE);
where()中的NULL,FALSE告诉CodeIgniter不要转义查询。
或者您可以使用此查询而不是子查询来使用JOIN。
答案 1 :(得分: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();
}