CodeIgniter生成的语句未按预期给出结果


CodeIgniter generated Statement does not give results as expected

我已经打印出了一个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"的结果。为什么它不能正常工作?

运算符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'

此查询将只返回具有居住许可的申请人。

您可以试试这个(CI支持自定义字符串)

$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