简体   繁体   English

MySQL子查询的结果不正确

[英]Inaccurate results with MySQL Subquery

This is the query created by my PHP script . 这是我的PHP脚本创建的查询。 The query executes successfully without any errors, but with results i am not expecting. 查询成功执行,没有任何错误,但结果并不理想。 I want the rows in the result of this query to contain all the three keywords (3D,design,PAG). 我希望此查询结果中的行包含所有三个关键字(3D,design,PAG)。 But, Right now it returns results even if one key word is found. 但是,即使找到一个关键字,它现在也会返回结果。 How can the query be Modified ? 如何修改查询?

SELECT ID
FROM  `catiascripts` 
WHERE  `ID` LIKE  '%3D%'
OR  `ScriptName` LIKE  '%3D%'
OR  `ScriptApplication` LIKE  '%3D%'
OR  `ScriptDescription` LIKE  '%3D%'
OR  `Customer` LIKE  '%3D%'
AND ID
IN (

SELECT ID
FROM  `catiascripts` 
WHERE  `ID` LIKE  '%design%'
OR  `ScriptName` LIKE  '%design%'
OR  `ScriptApplication` LIKE  '%design%'
OR  `ScriptDescription` LIKE  '%design%'
OR  `Customer` LIKE  '%design%'
)
AND ID
IN (

SELECT ID
FROM  `catiascripts` 
WHERE  `ID` LIKE  '%PAG%'
OR  `ScriptName` LIKE  '%PAG%'
OR  `ScriptApplication` LIKE  '%PAG%'
OR  `ScriptDescription` LIKE  '%PAG%'
OR  `Customer` LIKE  '%PAG%'
)
LIMIT 0 , 30

You need brackets on your and/or expression. 您在and/or表达式上需要使用方括号。 You're violating MySQL's order-of-operations, therefore you must enforce your own with appropriate () . 您违反了MySQL的操作顺序,因此必须使用适当的()强制执行自己的操作。

and has a higher priority than or , so your query: and具有比or更高的优先级,因此您的查询:

SELECT   p OR q OR r AND x OR y OR z ...

is being executed as 被执行为

SELECT p OR q OR (r AND x) OR y OR Z

You want 你要

SELECT (p OR q OR r) AND (x OR y OR Z) AND (...)

Relevant doc: https://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html 相关文档: https : //dev.mysql.com/doc/refman/5.0/en/operator-precedence.html

SELECT ID
FROM `catiascripts` 
WHERE (
       `ID` LIKE '%3D%'
    OR `ScriptName` LIKE '%3D%'
    OR `ScriptApplication` LIKE '%3D%'
    OR `ScriptDescription` LIKE '%3D%'
    OR `Customer` LIKE '%3D%'
)
AND (
       `ID` LIKE '%design%'
    OR `ScriptName` LIKE '%design%'
    OR `ScriptApplication` LIKE '%design%'
    OR `ScriptDescription` LIKE '%design%'
    OR `Customer` LIKE '%design%'
)
AND (
       `ID` LIKE '%PAG%'
    OR `ScriptName` LIKE '%PAG%'
    OR `ScriptApplication` LIKE '%PAG%'
    OR `ScriptDescription` LIKE '%PAG%'
    OR `Customer` LIKE '%PAG%'
)
LIMIT 0 , 30

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

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