【发布时间】:2016-09-22 11:39:43
【问题描述】:
我正在尝试这个:
$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 位客人和国家阿尔巴尼亚提供优惠(每桌 1 排)。但是,如果$guests = 2; 我有这 1 行的结果。如果我使用like 和or_like 代替where 和or_where,也是一样的。如果我评论这一行:
$this->db->or_where('l.city LIKE', $location.'%');
一切正常,如果$guests != 3 没有结果,如果$guests = 3 有 1 行结果。
使用$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
如何进行此查询?
选择已完成 = 1、客人 = $guests 和城市或国家/地区(如 $location)的值。
【问题讨论】:
标签: php mysql codeigniter activerecord codeigniter-3