php - 使用 where、like 和 or_like CodeIgniter 3 Active Records 进行多重连接

标签 php mysql codeigniter activerecord codeigniter-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->where('l.country LIKE', $location.'%');
        $this->db->or_where('l.city LIKE', $location.'%');
        $this->db->limit(5);

我有 3 位客人和阿尔巴尼亚国家的报价(每 table 1 行)。但是,如果 $guests = 2; 我有这 1 行的结果。如果我使用 likeor_like 而不是 whereor_where 也是一样的。如果我评论这一行:

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

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

使用 $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

我该如何查询? 选择 completed = 1、客人 = $guests 和城市或国家/地区(如 $location)的值。

最佳答案

试试这段代码

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

关于php - 使用 where、like 和 or_like CodeIgniter 3 Active Records 进行多重连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39637999/

相关文章:

php - 如何在 laravel 中减去时间输入之间的差异?

mysql - 使用 restful API 在 nodejs 上实现 breeze

php - 在Mysql中插入PHP值后如何显示最后选择的下拉值?

php - cakePHP 3.0 - 在同一 View 中以不同方式显示图像 - querybuilder

codeigniter - CodeIgniter 中的自定义异常

PHP - 从目录中读取大量文件

javascript - 使用 jquery 从运行 mysql 的 php 脚本返回信息

php - PHP 平均排名

php - 相当于 PHP multi_query 的 Codeigniter 是什么

php - 在 Codeigniter 框架中使用 Grocery 凝乳库的优点和缺点。