带有几个括号的SQL语句

时间:2013-05-01 11:48:11

标签: sql brackets

我有一个请求,其中有几个与OR相关的条件。在模块中:

$array = array('hostel.postal_code' => $filter_state, 'region.name' => $filter_state, 'hostel.town' => $filter_state, 'state.name' => $filter_state);
$this->db->or_like($array);

我看到sql:

WHERE `bp_hostel`.`status` =  'A'
AND `bp_hostel`.`postal_code`  LIKE '%Vic%'
OR  `bp_region`.`name`  LIKE '%Vic%'
OR  `bp_hostel`.`town`  LIKE '%Vic%'
OR  `bp_state`.`name`  LIKE '%Vic%'
ORDER BY `view_count` desc

我对请求有疑问,我想设置“(...)”以确保我的sql是正确的,例如:

WHERE `bp_hostel`.`status` =  'A'
AND ( `bp_hostel`.`postal_code`  LIKE '%Vic%'
OR  `bp_region`.`name`  LIKE '%Vic%'
OR  `bp_hostel`.`town`  LIKE '%Vic%'
OR  `bp_state`.`name`  LIKE '%Vic%' )
ORDER BY `view_count` desc

我在doc中读到:

You can wrote your requests by hands:
$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where);

但我发现这个变种不方便,因为我不喜欢将所有条件都写成1个字符串。在此Methods方法中,还有一些SQL条件...

如何制作?

0 个答案:

没有答案