sql 逻辑运算符 优先级
SELECT *
FROM tl_documentation
WHERE storehouse_id =2
OR customer_id =2
AND product_id =20
ORDER BY id DESC
LIMIT 0 , 30
FROM tl_documentation
WHERE storehouse_id =2
OR customer_id =2
AND product_id =20
ORDER BY id DESC
LIMIT 0 , 30
以下列表显示了操作符优先级的由低到高的顺序。排列在同一行的操作符具有相同的优先级。 := ||, OR, XOR &&, AND NOT BETWEEN, CASE, WHEN, THEN, ELSE =, <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN | & <, >> -, + *, /, DIV, %, MOD ^ - (一元减号), ~ (一元比特反转) ! BINARY, COLLATE |
于优先级高于或,那customer_id =2 AND product_id =20就先执行,然后再和第一个表达式或,全混了,当然得不到想要的结果,加上一个小括号就OK了
SELECT *
FROM tl_documentation
WHERE (
storehouse_id =2
OR customer_id =2
)
AND product_id =20
ORDER BY id DESC
FROM tl_documentation
WHERE (
storehouse_id =2
OR customer_id =2
)
AND product_id =20
ORDER BY id DESC