【问题标题】:sql query with like and or combination带有like and or组合的sql查询
【发布时间】:2013-11-02 22:22:15
【问题描述】:

我需要在 codeigniter 中执行类似这个 sql 查询的操作:

SELECT `table`.* FROM (`table`) WHERE `name` LIKE '%9%' OR `id` LIKE '%9%' AND (`name` LIKE '%f%' OR `id` LIKE '%f%')

我尝试使用这段代码:

$this->db->select('table.*');
$this->db->from('table');
foreach($data as $d){
    $this->db->like("name", $d)
    $this->db->or_like("id", $d);
}

但是sql中的结果是:

SELECT `table`.* FROM (`table`) WHERE `name` LIKE '%9%' OR `id` LIKE '%9%' AND `name` LIKE '%f%' OR `id` LIKE '%f%'

谢谢

【问题讨论】:

    标签: sql codeigniter where combinations


    【解决方案1】:

    CI v2 中没有括号。您需要使用 where() 函数,例如:

     ->where('mysql in here', null, true);
    

    在这种情况下,null 只是一个占位符; TRUE 不会添加 ` (反引号),这通常会妨碍此处。您可以在用户指南中查看更多信息

    【讨论】:

      【解决方案2】:
      $this->db->select('*');
      $this->db->from('tablename');
      $this->db->like('name','9','both');
      $this->db->or_like('id','9','both');
      $this->db->like('name','f','both');
      $this->db->or_like('id','f','both');
      

      【讨论】:

        猜你喜欢
        • 2015-03-25
        • 2018-06-03
        • 1970-01-01
        • 2011-05-08
        • 1970-01-01
        • 2012-06-18
        • 2020-07-11
        • 1970-01-01
        相关资源
        最近更新 更多