MySQL查询 - 不等于此和此

时间:2022-01-02 18:58:57

I'm getting some weird results, so I need to just check myself...

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

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

To be clear, these are allowed:

需要说明的是,这些是允许的:

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

THis is NOT allowed to be returned from my query:

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

pending = 1, complete = 0

What am I missing here...?

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

2 个解决方案

#1


35  

Try:

尝试:

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

or

要么

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

EDIT: I went and looked at: http://dev.mysql.com/doc/refman/4.1/en/expressions.html

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

#2


9  

You need to use OR, not AND. Your expression leaves out any combination where complete = 0 or pending = 1, which is too restrictive. Try the following:

你需要使用OR,而不是AND。你的表达式中没有完成= 0或pending = 1的任何组合,这是限制性太强。请尝试以下方法:

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

Sample: http://sqlize.com/G8j6sFqo09

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

#1


35  

Try:

尝试:

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

or

要么

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

EDIT: I went and looked at: http://dev.mysql.com/doc/refman/4.1/en/expressions.html

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

#2


9  

You need to use OR, not AND. Your expression leaves out any combination where complete = 0 or pending = 1, which is too restrictive. Try the following:

你需要使用OR,而不是AND。你的表达式中没有完成= 0或pending = 1的任何组合,这是限制性太强。请尝试以下方法:

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

Sample: http://sqlize.com/G8j6sFqo09

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