【问题标题】:Multiple joins with where, like and or_like CodeIgniter 3 Active Records与 where、like 和 or_like CodeIgniter 3 Active Records 的多个连接
【发布时间】:2016-09-22 11:39:43
【问题描述】:

我正在尝试这个:

        $location = 'a';
        $this->db->select('l.id, d.guests, l.city, l.country');
        $this->db->from('offers as o');
        $this->db->join('location as l', 'l.id = o.id', 'left');
        $this->db->join('desc as d', 'd.offer_id = o.id', 'left');
        $this->db->where('d.guests', $guests);
        $this->db->where('o.completed', 1);
        $this->db->where('l.country LIKE', $location.'%');
        $this->db->or_where('l.city LIKE', $location.'%');
        $this->db->limit(5);

我已为 3 位客人和国家阿尔巴尼亚提供优惠(每桌 1 排)。但是,如果$guests = 2; 我有这 1 行的结果。如果我使用like 和or_like 代替where 和or_where,也是一样的。如果我评论这一行:

$this->db->or_where('l.city LIKE', $location.'%');

一切正常,如果$guests != 3 没有结果,如果$guests = 3 有 1 行结果。

使用$this-db->last_query() 生成的查询是:

SELECT 'l'.'id', 'd'.'guests', 'l'.'city', 'l'.'country' FROM 'offers' as 'o' LEFT JOIN 'location' as 'l' ON 'l'.'id' = 'o'.'id' LEFT JOIN 'desc' as 'd' ON 'd'.'id' = 'o'.'id' WHERE 'd'.'guests' = 3 AND 'o'.'completed' = 1 AND 'l'.'country' LIKE 'a%' OR 'l'.'city' LIKE 'a%' LIMIT 5

如何进行此查询? 选择已完成 = 1、客人 = $guests 和城市或国家/地区(如 $location)的值。

【问题讨论】:

    标签: php mysql codeigniter activerecord codeigniter-3


    【解决方案1】:

    试试这个代码

    $location = 'a';
    $this->db->select('l.id, d.guests, l.city, l.country');
    $this->db->from('offers as o');
    $this->db->join('location as l', 'l.id = o.id', 'left');
    $this->db->join('desc as d', 'd.offer_id = o.id', 'left');
    $this->db->where('d.guests', $guests);
    $this->db->where('o.completed', 1);
    $this->db->where("(l.country LIKE '".$location."%' or l.city LIKE '".$location."%')");
    $this->db->limit(5);
    

    【讨论】:

    • You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '% or l.city` LIKE a%) LIMIT 5' `
    【解决方案2】:

    你需要使用Query grouping

    $location = 'a';
    $this->db->select('l.id, d.guests, l.city, l.country');
    $this->db->from('offers as o');
    $this->db->join('location as l', 'l.id = o.id', 'left');
    $this->db->join('desc as d', 'd.offer_id = o.id', 'left');
    $this->db->where('d.guests', $guests);
    $this->db->where('o.completed', 1);
    $this->db->->group_start();
    $this->db->where('l.country LIKE', $location.'%');
    $this->db->or_where('l.city LIKE', $location.'%');
    $this->db->group_end();
    $this->db->limit(5);
    

    另外,请查看like and or_like 方法以避免类似'l.country LIKE' 的代码。

    【讨论】:

      【解决方案3】:

      试试这个

      $location = 'a';
       $this->db->select('l.id, d.guests, l.city, l.country');
       $this->db->from('offers as o');
       $this->db->join('location as l', 'l.id = o.id', 'left');
       $this->db->join('desc as d', 'd.offer_id = o.id', 'left');
       $this->db->where('d.guests', $guests);
       $this->db->where('o.completed', 1);
       $this->db->like('l.country',$location,'after');
       $this->db->or_like('l.city',$location,'after');
       $this->db->limit(5);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-10
        • 2013-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多