douzhanrun0497 2013-12-01 10:14
浏览 21

CodeIgniter生成的Statement没有按预期给出结果

I have printed out a CI generated code, so to talk about the statement, since it would be easier. Here it is:

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

But, it shows results which are with value "8" for the province, while it is said in statement to only get results with province "5". Why it does not work correctly?

  • 写回答

2条回答 默认 最新

  • dongye9228 2013-12-01 10:32
    关注

    The operator and has a higher precedence than the operator or. That means that if you query:

    select  *
    from    applicants
    where   expected_salary < 10
            or knowledge = 'high' 
            and has_residency_permit = 'true'
    

    you could get someone with a low salary without a residency permit. The solution is to use parenthesis to override precedence:

    select  *
    from    applicants
    where   (expected_salary < 10
            or knowledge = 'high')
            and has_residency_permit = 'true'
    

    This query will only return applicants with a residency permit.

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)