我有这个SQL WHERE语句:
WHERE code = @code OR approvedby = @approvedby AND [status] = 'APPROVED'
我想显示已批准状态的信息code
和approvedby
参数为true且状态必须为APPROVED,但问题是PENDING状态也会显示。
有人可以解释我是否犯了错误吗?
答案 0 :(得分:3)
我猜你只是缺少括号:
WHERE (code = @code OR approvedby = @approvedby) AND
[status] = 'APPROVED'
答案 1 :(得分:3)
您的查询中存在模糊逻辑,因此计算机可能处理的方式与您的预期不同。尝试将括号放在您需要的位置。
A or B and C - ambiguous
(A or B) and C - A or B can be true, and C is true
A or (B and C) - either A is true, or B and C are true together
你可能想要:
WHERE
(code = @code OR approvedby = @approvedby)
AND
[status] = 'APPROVED'