我有一个简单的SQL查询:
SELECT
w.fizz
FROM
widgets w
WHERE
w.special_id = 2394
AND w.buzz IS NOT NULL
AND w.foo = 12
在伪代码中,这个WHERE
子句可以被认为是:
if(specialId == 2394 && buzz != null && foo == 12)
我现在想要更改此查询,以便返回其special_id为2394且其buzz不为null且其foo为12, OR ,其special_id为2394且其等等的所有小部件是'是',其数字是4.在伪代码中:
if(specialId == 2394 && (buzz != null && foo == 12) || (blah == "YES" && num == 4))
我尝试了以下操作,只是为了得到错误:
SELECT
w.fizz
FROM
widgets w
WHERE
w.special_id = 2394
AND
(
w.buzz IS NOT NULL
AND w.foo = 12
)
OR
(
w.blah = 'YES'
AND w.num = 4
)
有什么想法吗?提前谢谢!
答案 0 :(得分:0)
SELECT
w.fizz
FROM
widgets w
WHERE
w.special_id = 2394
AND
(
(
w.buzz != null
AND w.foo = 12
)
OR
(
w.blah = 'YES'
AND w.num = 4
)
)
在“OR”周围添加其他括号,因为“OR”的优先级低于“AND”。