我有以下SQL语句:
SELECT * FROM `table` WHERE type = 3 OR type = 5 OR type = 4 and table.deleted = 1;
我读过我可以使用括号来完成此操作,但我想知道这是否有效:
SELECT * FROM `table` WHERE (type = 3 OR type = 5 OR type = 4) and table.deleted = 1;
或者
SELECT * FROM `table` WHERE (type = 3 OR type = 5) OR type = 4 and table.deleted = 1;
这两个都是有效的,但是由于AND优先级高于OR,因此它们的含义有所不同:
AND
OR
您可以使用operator完全避免混淆IN,如下所示:
IN
SELECT * FROM `table` WHERE type IN (3, 4, 5) AND table.deleted = 1;
或者如果您想要第二个意思
SELECT * FROM `table` WHERE type IN (3, 5) OR (type = 4 AND table.deleted = 1)