[英]mysql query where clause using AND and OR
Can someone help me with this query: 有人可以帮我这个查询:
SELECT SQL_CALC_FOUND_ROWS *
FROM Punches
WHERE Serial LIKE '%123%'
OR Badge LIKE '%123%'
OR Search_Date LIKE '%123%'
OR Punch_Time LIKE '%123%'
OR Device_Name LIKE '%%'
AND Company_ID = '0002'
LIMIT 0, 20
Essentially I want to search all these fields that are LIKE 123 BUT have to have Company_ID = '0002'. 本质上,我想搜索所有类似LIKE 123但必须具有Company_ID ='0002'的字段。 This query returns rows that do not have a Company_ID = '0002'
该查询返回没有Company_ID ='0002'的行
Try this... 尝试这个...
SELECT
SQL_CALC_FOUND_ROWS *
FROM
Punches
WHERE
(Company_ID = '0002') AND
(Serial LIKE '%123%'
OR Badge LIKE '%123%'
OR Search_Date LIKE '%123%'
OR Punch_Time LIKE '%123%'
OR Device_Name LIKE '%%')
LIMIT 0, 20
1) AND
has a higher priority than OR
. 1)
AND
的优先级高于OR
。 Just add parentheses: 只需加上括号即可:
SELECT SQL_CALC_FOUND_ROWS *
FROM Punches
WHERE (... OR ... OR ...)
AND Company_ID = '0002'
LIMIT 0, 20
2) Device_Name LIKE '%%'
is always true. 2)
Device_Name LIKE '%%'
始终为true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.