我正在尝试:
$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 行的结果。如果我使用 like
和 or_like
而不是 where
和 or_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/