我有以下代码:
$this->db->from('addresses');
$this->db->where('user_id', $this->session->userdata('user.id'));
$this->db->like('country', $pSearch);
$this->db->or_like('state', $pSearch);
$this->db->or_like('city', $pSearch);
$this->db->or_like('zip', $pSearch);
它生成以下SQL
SELECT *
FROM (`addresses`)
WHERE `user_id` = '1'
AND `country` LIKE '%d%'
OR `state` LIKE '%d%'
OR `city` LIKE '%d%'
OR `zip` LIKE '%d%'
ORDER BY `country` asc
LIMIT 15
有没有办法让它像这样生成它:
SELECT *
FROM (`addresses`)
WHERE `user_id` = '1'
AND (`country` LIKE '%d%'
OR `state` LIKE '%d%'
OR `city` LIKE '%d%'
OR `zip` LIKE '%d%')
ORDER BY `country` asc
LIMIT 15
因为我只想获得user_id = 1的记录而不是所有用户的记录.
最佳答案 这似乎是CI ActiveRecord中的错误/限制.您可以尝试
Ignited Query,它能够处理嵌套的WHERE子句,并且可能可以按您喜欢的方式处理嵌套的WHERE / LIKE.