代码之家  ›  专栏  ›  技术社区  ›  C. E.

Codeigniter活动记录与几个类似或?

  •  2
  • C. E.  · 技术社区  · 15 年前

    嘿。。在我的sql查询(使用Codeigniter的activerecord生成)中使用几个“like”时遇到了问题:

    SELECT * FROM (`posts`) WHERE `city` LIKE '%test%' AND `title` LIKE '%%' OR `text` LIKE '%%'
    

    问题是,它看起来好像在第一个like和第二个like之间有一个圆括号,但是我希望在第二个like和最后一个like之间有一个圆括号(我希望它能比较最后一个like和下一个like是否起作用)。

    当前代码: 如果($type!=0)

        $this->db->like('city', $area);
        $this->db->like('title', $words);
        $this->db->or_like('text', $words);
    
        return $this->db->get('posts')->result_array(); 
    
    4 回复  |  直到 15 年前
        1
  •  4
  •   musoNic80    15 年前

    我认为CI没有添加任何偏执。它将在前两个语句之间添加“AND”,在后两个语句之间添加“OR”。为了实现你的愿望,我会写我自己的声明。这很简单。

    $sql = SELECT * FROM 'posts' WHERE city LIKE ? AND ( title LIKE ? OR text LIKE ? );
    $query = $this->db->query($sql, array($area, $words, $words));
    

        2
  •  20
  •   Kamran Jabbar    8 年前

    前面的答案是正确的。我想补充一下。。。

    在Codeigniter中支持以下总体。。。

    $this->db->like();
    $this->db->or_like();
    $this->db->not_like();
    $this->db->or_not_like();
    

    希望这能帮助别人。

        3
  •  4
  •   Murat Akdeniz    9 年前

    这样就行了

    $this->db->select('*')->from('my_table')
            ->group_start()
                    ->where('a', 'a')
                    ->or_group_start()
                            ->where('b', 'b')
                            ->where('c', 'c')
                    ->group_end()
            ->group_end()
            ->where('d', 'd')
    ->get();
    
    // Generates:
    // SELECT * FROM (`my_table`) WHERE ( `a` = 'a' OR ( `b` = 'b' AND `c` = 'c' ) ) AND `d` = 'd'
    
        4
  •  0
  •   Harshan Morawaka    8 年前

    public function get_all_airports($para) {
    
            $this->db->select('airports.name as air_name, airports.city, airports.type, airports.home_link, country.name as con_name');
            $this->db->from('airports');
    
            $this->db->join('country', 'airports.iso_country = country.code', 'inner');
    
            $this->db->where("airports.type !=", "small_airport");
            $this->db->where("airports.type !=", "closed");
            $this->db->where("airports.type !=", "heliport");
            $this->db->where("airports.type !=", "medium_airport");
            $this->db->where("airports.type !=", "seaplane_base");
    
            $where = "(country.name LIKE '%$para%' OR airports.city LIKE '%$para%')";
            $this->db->where($where);
    
            echo json_encode($this->db->limit(20)->get()->result());
        }
    
    推荐文章