dongxun5349 2016-09-22 11:39
浏览 37

多个连接与whereI,like和or_like CodeIgniter 3 Active Records

I am trying this:

        $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);

And I have offer with 3 guests and with country Albania (1 row per table for it). But, if $guests = 2; I have result this 1 row. It is same, if I use like and or_like instead where and or_where. If I comment this line:

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

all works fine, I have no results if $guests != 3 and 1 row result if $guests = 3.

Generated query with $this-db->last_query() is:

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

How can I make this query ? Select values where completed = 1, guests = $guests and city or country like $location.

  • 写回答

3条回答 默认 最新

  • doudong1117 2016-09-22 11:53
    关注

    try this code

    $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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答