MySQL里AND和OR的优先级问题

-- MySQL中,AND的执行优先级高于OR,在没有小括号()的干预下,总是优先执行AND语句,再执行OR语句。

SELECT *
FROM table1
WHERE f1 = 1 AND f2 = 2
   OR f3 = 3 AND f4 = 4
   OR f5 = 5;

-- 上面的SQL等价于

SELECT *
FROM table1
WHERE (f1 = 1 AND f2 = 2)
   OR (f3 = 3 AND f4 = 4)
   OR f5 = 5;

Copyright © 2024 码农人生. All Rights Reserved