我有一个SQL语句,如果它们属于某个用户,只要它们的条件1不是1或者条件2不是1,就应该返回行。但是当我执行条件1或条件2只有1时 - 行是仍然回来了。我试图这样做,如果condition1或condition2或两者都是1然后不返回这样的行,但由于某种原因它只有在两个条件= 1时才有效
Select * FROM table1
WHERE table1.user = $user
AND (condition1 = 0 OR condition2 = 0)
答案 0 :(得分:3)
您的要求是矛盾的。以下是每种方案的一些解决方案:
属于某个用户,只要他们的condition1不是1 OR condition2不是1
如果condition1和condition2不可为空:
Select * FROM table1
WHERE table1.user = $user
AND (condition1 <> 1 OR condition2 <> 1)
如果是:
Select * FROM table1
WHERE table1.user = $user
AND (condition1 <> 1 OR condition2 <> 1
OR condition1 IS NULL OR condition2 IS NULL)
如果condition1或condition2或两者都是1则不返回
不可空:
Select * FROM table1
WHERE table1.user = $user
AND condition1 <> 1 AND condition2 <> 1
可为空:
Select * FROM table1
WHERE table1.user = $user
AND ((condition1 <> 1 OR condition1 IS NULL)
AND (condition2 <> 1 OR condition2 IS NULL))
答案 1 :(得分:1)
你的逻辑有点倒退,试试这个:
Select * FROM table1
WHERE table1.user = $user
AND NOT (condition1 = 0 OR condition2 = 0)