简体   繁体   English

大于运算符无法在查询中工作

[英]Greater than operator not working in a query

Could anyone suggest why the greater than operator is being ignored in this MySQL query? 谁能提出建议,为什么在此MySQL查询中忽略大于运算符?

$sql = "SELECT *
FROM `items`
WHERE `short_description` LIKE '%".$term."%'
    OR `description` LIKE '%".$term."%'
    AND `quantity` > 0
ORDER BY year DESC, denomination ASC, description ASC $max";

I have a similar query on the same site that works 我在可以正常使用的同一网站上进行了类似的查询

$sql = "SELECT *
FROM `items`
WHERE `category` = '".$cat_id."'
    AND `quantity` > 0
ORDER BY year DESC, denomination ASC, description ASC;";

Everything works well, except the quantity comparison on the first query, Its has got me stumped. 一切正常,除了第一个查询的数量比较,它让我感到困惑。

you should group your OR and AND conditions by enclosing them in a parenthesis, 您应将“ OR和“ AND条件分组,并用括号括起来,

$sql = "SELECT * FROM `items` 
         WHERE  (`short_description` LIKE '%".$term."%' OR 
                 `description` LIKE '%".$term."%')  AND 
                  `quantity` > 0 
          ORDER BY year DESC, denomination ASC, description ASC $max";

followup question: is $max a variable which contains LIMIT clause? 跟进问题: $max是一个包含LIMIT子句的变量吗?

As a sidenote, the query is vulnerable with SQL Injection if the value( s ) of the variables came from the outside. 作为一个旁注,查询是脆弱的SQL Injection ,如果变量的值(一个或多个 )从外面走了进来。 Please take a look at the article below to learn how to prevent from it. 请查看下面的文章,以了解如何防止它。 By using PreparedStatements you can get rid of using single quotes around values. 通过使用PreparedStatements您可以摆脱在值周围使用单引号的麻烦。

"SELECT * FROM items WHERE (short_description LIKE '%".$term."%' OR description LIKE 
'%".$term."%') 
AND quantity > 0 
ORDER BY year DESC, denomination ASC, description ASC $max";

Try that... You have an Or condition, and an and. 尝试...您有一个Or条件,一个and条件。

Try 尝试

$sql = "SELECT *
FROM `items`
WHERE (`short_description` LIKE '%".$term."%'
    OR `description` LIKE '%".$term."%')
    AND `quantity` > 0
ORDER BY year DESC, denomination ASC, description ASC $max";

I am thinking that your OR statement is the problem. 我认为您的OR陈述式就是问题。

It's not being ignored, it's just that you misunderstand how it's being applied. 它不会被忽略,只是您误解了它的应用方式。 The expression: 表达方式:

WHERE `short_description` LIKE '%".$term."%' (call this XX,)
OR `description` LIKE '%".$term."%'          (          YY,)
AND `quantity` > 0                           (      and ZZ.)

is interpreted as: 被解释为:

where XX or (YY and ZZ)

whereas what you probably want is: 而您可能想要的是:

where (XX or YY) and ZZ

Hence you have to override the default interpretation as follows: 因此,您必须重写默认解释,如下所示:

WHERE (`short_description` LIKE '%".$term."%' OR
       `description` LIKE '%".$term."%')
  AND `quantity` > 0

The reason your second query doesn't have this problem is because you're not mixing AND and OR . 您的第二个查询没有此问题的原因是因为您没有混合ANDOR


In addition, though unrelated to your specific question), you should be very wary of double-ended like clauses such as like '%something%' . 另外,尽管与您的特定问题无关),但您应非常警惕诸如like '%something%'的双头like子句。 They're real performance killers for decent sized tables and there are ways to improve the performance considerably. 它们是大小合适的表的真正性能杀手,并且有很多方法可以显着提高性能。

It may not matter in this case if your tables are small but it's something to keep in mind. 在这种情况下,如果您的桌子很小,可能没关系,但是要记住这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM