MySQL查询 - 不等于此和此

时间:2011-11-15 00:52:29

标签: mysql

我得到一些奇怪的结果,所以我需要检查自己......

SELECT *
FROM table
WHERE complete != 0
AND pending != 1

要明确,这些是允许的:

pending = 0, complete = 0
pending = 1, complete = 1
pending = 0, complete = 1

不允许从我的查询中返回:

pending = 1, complete = 0

我在这里想念的是什么......?

2 个答案:

答案 0 :(得分:37)

尝试:

SELECT *
FROM table
WHERE NOT (complete = 0
AND pending = 1)

SELECT *
FROM table
WHERE !(complete = 0
AND pending = 1)

编辑: 我去看了看: http://dev.mysql.com/doc/refman/4.1/en/expressions.html

答案 1 :(得分:9)

您需要使用OR,而不是AND。您的表达式会遗漏complete = 0pending = 1的任何组合,这种组合限制性太强。请尝试以下方法:

SELECT *
FROM Table
WHERE complete != 0 OR pending != 1;
                    ^^ change AND to OR

示例:http://sqlize.com/G8j6sFqo09