微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 如何将WHERE子句与活动记录分组?

下面的活动记录代码没有像我需要的那样对WHERE子句进行分组……

        $this->db->where('customers_id',$this->user->getCurrentUserProperty('id'))
        $this->db->like('delivery_name', $this->input->post('search'));
        $this->db->or_like('customers_company', $this->input->post('search'));
        $this->db->or_like('customers_company2', $this->input->post('search'));
        $this->db->or_like('customers_company3', $this->input->post('search'));
        $this->db->or_like('customers_company4', $this->input->post('search'));

这会产生

WHERE `customers_id` = 31509 AND `delivery_name` LIKE '%str%' OR `customers_company` LIKE '%str%'"

我需要输出在它周围括号括起来……我怎么能这样做?

WHERE `customers_id` = 31509 AND (`delivery_name` LIKE '%str%' OR `customers_company` LIKE '%str%')"

注意:我讨厌像这样烦人时间的活跃记录

如果我使用下面的方法,我如何清理输入,因此它不是原始的查询

$where = "name='Joe' AND status='boss' OR status='active'";

$this->db->where($where);

解决方法:

不幸的是,似乎没有一种干净的方法来做到这一点. where接受一个字符串作为子句,所以你可以这样做(是的,我知道,它不漂亮):

$search = $this->db->escape_like_str($this->input->post('search'));
$this->db->where('customers_id',$this->user->getCurrentUserProperty('id'));
$this->db->where("(`delivery_name` LIKE '%$search%' OR `customers_company` LIKE '%$search%')");

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐