MySQL 查询 - 不等于 THIS 和 THIS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8130221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:41:34  来源:igfitidea点击:

MySQL Query - Not Equal to THIS and THIS

mysql

提问by Shackrock

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...?

我在这里错过了什么......?

回答by David Parvin

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

回答by mellamokb

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

您需要使用OR,而不是AND。您的表达式忽略了 wherecomplete = 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