MySQL Codeigniter 有几个像或这样的活动记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3489399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:52:56  来源:igfitidea点击:

Codeigniter active record with several like or?

activerecordcodeignitermysql

提问by C. E.

Hey.. I've run into a problem using several "like" in my sql-query (generated with Codeigniter's activerecord):

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

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

Thing is, it appears to read this query as if there was a parenthesis around the first like and the second like, but I want there to be a parenthesis around the second and the last like (I want it to compare if the last or the next to last works).

事情是,它似乎读取这个查询,好像第一个喜欢和第二个喜欢周围有一个括号,但我希望在第二个和最后一个喜欢周围有一个括号(我希望它比较最后一个还是在最后的作品旁边)。

How can I achieve this using Codeigniter's Active Record class?

如何使用 Codeigniter 的 Active Record 类实现这一点?

Current code: if($type != 0) $this->db->where('type', $type);

当前代码: if($type != 0) $this->db->where('type', $type);

    $this->db->like('city', $area);
    $this->db->like('title', $words);
    $this->db->or_like('text', $words);

    return $this->db->get('posts')->result_array(); 

回答by Anish Charles

Previous answer is correct. I would like to add the following...

之前的答案是正确的。我想添加以下内容...

In Codeigniter supporting the following overall...

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

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

Hope this will help someone.

希望这会帮助某人。

回答by musoNic80

I don't think CI is adding any paranthesis. It will add an 'AND' between the first two statements and an 'OR' between the last two. To achieve what you want, I would write my own statement. It's very straightforward.

我认为 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));

Notice how I've used binding. This automatically escapes characters for you.

请注意我如何使用绑定。这会自动为您转义字符。

回答by Murat Akdeniz

this will do the trick

这将解决问题

$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'

回答by user3444748

when you have to use table join as well try following method

当您必须使用表连接时,请尝试以下方法

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());
    }