ci框架 用框架自带db 添加括号,比如 like 等等左右添加括号 解决办法

 1         $this->load->model('station/Station_model','Station');
 2         // East
 3         // North
 4         $this->Station->set_where('isdel',0); 
 5         //连表查询地区  
 6         $this->Station->list_attributes = 'region.text as sitone,station.North,station.East,station.state,station.stabianhao,station.staname,station.siteonename,station.sitetwoname,station.sitename,station.statype,station.settime,station.staid';
 7         $this->Station->join('region','region.linkageid = station.sitetwoname','left');
 8         if ($type!='all') {
 9             $this->Station->set_where('station.state',$type);
10         }
11 
12         $this->Station->set_where(' (  station.sitetwoname',$sitearr,'where_in');
13         $this->Station->set_where('station.siteonename',$sitearr,'or_where_in');
14         $this->Station->set_where('  1 = 2 ) and 1','1','or_where_in');
15 
16         $data = $this->Station->get_stewhere_all();
17         echo $this->db->last_query();die;

这样生成的sql为

 1 SELECT
 2     `dz_region`.`text` AS `sitone`,
 3     `dz_station`.`North`,
 4     `dz_station`.`East`,
 5     `dz_station`.`state`,
 6     `dz_station`.`stabianhao`,
 7     `dz_station`.`staname`,
 8     `dz_station`.`siteonename`,
 9     `dz_station`.`sitetwoname`,
10     `dz_station`.`sitename`,
11     `dz_station`.`statype`,
12     `dz_station`.`settime`,
13     `dz_station`.`staid`
14 FROM
15     `dz_station`
16 LEFT JOIN `dz_region` ON `dz_region`.`linkageid` = `dz_station`.`sitetwoname`
17 WHERE
18     `isdel` = 0
19 AND (
20     `dz_station`.`sitetwoname` IN (
21         '1',
22         '199',
23         '203',
24         '197',
25         '209',
26         '208'
27     )
28     OR `dz_station`.`siteonename` IN (
29         '1',
30         '199',
31         '203',
32         '205',
33         '211',
34         '197',
35         '209',
36         '208'
37     )
38     OR 1 = 2
39 )
40 AND 1 IN ('1')

以上

如果不采用我的办法生成的sql为

SELECT
	`dz_region`.`text` AS `sitone`,
	`dz_station`.`North`,
	`dz_station`.`East`,
	`dz_station`.`state`,
	`dz_station`.`stabianhao`,
	`dz_station`.`staname`,
	`dz_station`.`siteonename`,
	`dz_station`.`sitetwoname`,
	`dz_station`.`sitename`,
	`dz_station`.`statype`,
	`dz_station`.`settime`,
	`dz_station`.`staid`
FROM
	`dz_station`
LEFT JOIN `dz_region` ON `dz_region`.`linkageid` = `dz_station`.`sitetwoname`
WHERE
 `isdel` = 0

AND `dz_station`.`sitetwoname` IN (
	'1',
	'199',
	'203',
	'205',
	'197',
	'209',
	'208'
)
OR `dz_station`.`siteonename` IN (
	'1',
	'199',
	'203',
	'205',
	'211',
	'209',
	'208'
)

  当然这是不满足需求的,至于为什么不用原生sql,因为数组不好处理,

原文地址:https://www.cnblogs.com/zongsir/p/8040675.html