【问题标题】:CodeIgniter generated Statement does not give results as expectedCodeIgniter 生成的 Statement 未按预期给出结果
【发布时间】:2013-12-01 10:14:31
【问题描述】:
我已经打印了一个 CI 生成的代码,所以谈谈声明,因为它会更容易。这里是:
SELECT *
FROM (`ads`)
WHERE `status` = 2 AND `province` = '5' AND `title` LIKE '%شریف%'
OR `content` LIKE '%شریف%' OR `name` LIKE '%شریف%'
OR `keywords` LIKE '%شریف%'
ORDER BY `stars` DESC
但是,它显示的是省的值为“8”的结果,而声明中说只得到省为“5”的结果。为什么它不能正常工作?
【问题讨论】:
标签:
php
sql
codeigniter
search
【解决方案1】:
运算符and 的优先级高于运算符or。这意味着如果您查询:
select *
from applicants
where expected_salary < 10
or knowledge = 'high'
and has_residency_permit = 'true'
您可以在没有居留许可的情况下找到低薪的人。解决方法是使用括号覆盖优先级:
select *
from applicants
where (expected_salary < 10
or knowledge = 'high')
and has_residency_permit = 'true'
此查询将仅返回持有居留许可的申请人。
【解决方案2】:
你可以试试这个(CI 支持Custom string)
$where = "title LIKE '%شریف%' OR content LIKE '%شریف%' OR name LIKE '%شریف%' OR keywords LIKE '%شریف%'";
$this->db->select('*');
$this->db->from('ads');
$this->db->where($where);
$this->db->where("status = 2 AND province = '5'");
$this->db->order_by('stars', 'desc');
转到上面的给定链接并查找custom string。