简体   繁体   English

SQLite-WHERE子句中IS和=(等于)之间的差异。 (使用JDBC PreparedStatement)

[英]SQLite - Difference between IS and = (equals) in WHERE clause. (using JDBC PreparedStatement)

Edit: talking with a_horse_with_no_name I found that "IS" is a little bit different in SQLite allowing comparisons between NULL and values using "IS": stackoverflow.com/a/9102445/1470058 . 编辑:与a_horse_with_no_name交谈我发现SQLite中的“ IS”有点不同,允许使用“ IS”在NULL 值之间进行比较: stackoverflow.com/a/9102445/1470058 This clears up a lot of confusion for me. 这为我消除了很多困惑。 Thanks: 谢谢:

The IS and IS NOT operators work like = and != except when one or both of the operands are NULL. IS和IS NOT运算符的工作方式类似于=和!=,除非其中一个或两个操作数均为NULL。 In this case, if both operands are NULL, then the IS operator evaluates to 1 (true) and the IS NOT operator evaluates to 0 (false). 在这种情况下,如果两个操作数均为NULL,则IS运算符的计算结果为1(true),而IS NOT运算符的计算结果为0(false)。 If one operand is NULL and the other is not, then the IS operator evaluates to 0 (false) and the IS NOT operator is 1 (true). 如果一个操作数为NULL而另一个操作数为NULL,则IS运算符的计算结果为0(假),而IS NOT运算符的计算结果为1(真)。 It is not possible for an IS or IS NOT expression to evaluate to NULL. IS或IS NOT表达式无法计算为NULL。 Operators IS and IS NOT have the same precedence as =. 运算符IS和IS NOT的优先级与=相同。


I am confused about the keyword "IS" in SQLite. 我对SQLite中的关键字“ IS”感到困惑。

I'm working on a project that requires me to use Java's prepared Statements. 我正在一个需要我使用Java准备好的Statements的项目中。 I've come across two types of WHERE clauses: 我遇到了两种WHERE子句:

SELECT * FROM table WHERE column = ?

and

SELECT * FROM table WHERE column IS NULL

My question is there a difference a major between the equals sign "=" or the word "IS"? 我的问题是等号“ =”或单词“ IS”之间是否有主要区别? Google searches show that most people use = for value comparison and IS for comparing to null. Google搜索显示,大多数人使用=进行价值比较,使用IS进行与空值的比较。 However I attempted a few SQLite queries of my own. 但是我尝试了一些我自己的SQLite查询。

  • "IS" will return results as expected for " column IS NULL " and for " column IS value ". “ IS”将返回“ column IS NULL ”和“ column IS value ”的预期结果。
  • "=" will return results as expected for " column = value " but not for " column = NULL ". “ =”将返回“ column = value ”的预期结果,但不会返回column = NULL ”的结果。

My question is can I use "IS" for both situations without unexpected results? 我的问题是我是否可以在两种情况下都使用“ IS”而不会出现意外结果? I would like to make one prepared statement for a single query who's constraint on a column may or may not be null. 我想为一个对列的约束可能为null或不为null的查询做一个准备好的语句。 I hope I have been making sense. 我希望我一直有道理。

To simplify everything I said, can I use the following Java code without unexpected repercussions from using "IS": 为了简化我所说的一切,是否可以使用以下Java代码而不会因使用“ IS”而产生意外影响:

private static final String queryProjectSql = "SELECT * FROM fields WHERE project IS ?";
// later in a method
sqlStatement = connection.prepareStatement(queryProjectSql);
sqlStatement.setString(1, project); //Project may be a String or null

Thank You 谢谢

The above answers are getting it wrong. 上面的答案弄错了。 They're saying that '=' must be used in all cases and 'IS' for nulls. 他们说在所有情况下都必须使用'=',而对于空值则使用'IS'。 That's not what the documentation is saying. 那不是文档所说的。

The documentation is saying that the two operators are equivalent, the only difference being in how they evaluate null. 该文档说这两个运算符是等效的,唯一的区别在于它们对null的评估方式。 NULL on either side of = will evaluate to false. =两侧的NULL将评估为false。 IS will evaluate the truth of the NULL condition. IS将评估NULL条件的真实性。

This NULL = NULL will be false by the design of the operator and NULL is NULL will be true. 根据运算符的设计,此NULL = NULL将为false,而NULL为NULL将为true。

There are cases where you may not care if a column has a null value or has a legitimate doesn't match your query. 在某些情况下,您可能不在乎列是否为空值或合法字段与查询不匹配。 In which case you would use = ... and there are cases where you do care about null in which case you would use 'is' . 在这种情况下,您将使用= ...,并且在某些情况下您会关心null,在这种情况下,您将使用'is'。

SQL语言中的“ IS”仅在与NULL(IS NULL或IS NOT NULL)进行比较时使用,因为x = NULL将始终失败(NULL不等于任何值)

The "IS" keyword is strictly to be used with NULL value. 严格将“ IS”关键字与NULL值一起使用。 (eg. IS NULL or IS NOT NULL). (例如,IS NULL或IS NOT NULL)。

If you are looking for a match you need to use =. 如果要查找匹配项,则需要使用=。

If you are concerned about handling null column values then you should wrap the columns of concern with the isnull() function. 如果您担心处理空列值,则应使用isnull()函数包装所关注的列。

SELECT Col1
   , Col2
   , isnull(Col3,'') AS Col3
FROM myTable
Where col1 = 'somevalue'

I am not sure if you can use "Is" instead of "=" except for null but I can say you that you can not compare your column with null using "=" sign. 我不确定是否可以使用“ Is”代替“ =”,但null除外,但我可以说您不能使用“ =”符号将您的列与null进行比较。 you should use "Is null" or "Is not null". 您应该使用“为空”或“不为空”。 The reason is in SQL one null is not equals to another null. 原因是在SQL中,一个null不等于另一个null。 that means if you write something like null = null it will return you false. 这意味着,如果您编写类似null = null的内容,它将返回false。 So think of a case where you have column which does not contain anything that means in database it is null and you are trying to compare this null with another null using "=" which will never going to retun you true. 因此,请考虑以下情况:您的列不包含任何内容,这意味着在数据库中该列为null,并且您尝试使用“ =”将该null与另一个null进行比较,这将永远不会使您恢复为true。

Refer to Codd's Rule 3: Systematic treatment of null values. 请参阅Codd规则3:空值的系统处理。 It says one null is not equal to another null in relational database. 它说关系数据库中一个null不等于另一个null。

I am sharing this from wiki : http://en.wikipedia.org/wiki/Null_(SQL) 我正在通过Wiki分享此信息: http : //en.wikipedia.org/wiki/Null_(SQL)

For people who aren't database experts, a good way to remember what null means is to remember that in terms of information, "lack of a value" is not the same thing as "a value of zero"; 对于不是数据库专家的人来说,记住null的一种好方法是记住在信息方面,“缺少值”与“零值”不是一回事。 similarly, "lack of an answer" is not the same thing as "an answer of no". 同样,“缺少答案”与“没有答案”是不同的东西。 For example, consider the question "How many books does Juan own?" 例如,考虑以下问题:“胡安拥有几本书?” The answer may be "zero" (we know that he owns none) or "null" (we do not know how many he owns, or doesn't own). 答案可能是“零”(我们知道他不拥有)或“空”(我们不知道他拥有或不拥有多少)。 In a database table, the field reporting this answer would start out with a value of null, and it would not be updated with "zero" until we have ascertained that Juan owns no books. 在数据库表中,报告此答案的字段将以null值开头,并且在我们确定Juan不拥有任何书本之前,不会将其更新为“零”。 Similarly, when the question is, "Does Juanita own a car?", the answer "we don't know" is not the same thing as "no". 同样,当问题是“ Juanita是否拥有汽车吗?”时,答案“我们不知道”与“否”是不同的。 The former yields a database entry of "null"; 前者产生的数据库条目为“ null”。 only the latter yields a database entry of "no". 只有后者会产生“ no”的数据库条目。

Hope this will help !! 希望这会有所帮助!

如果列值为null,则=操作符与null比较时将永远不会返回true,因此NULL!= NULL,因此需要使用IS NULL进行测试。

In SQL, a comparison between a null value and any other value (including another null) a using a logical operator (eg =, !=, <, etc) will result in a null, which is considered as false for the purposes of a where clause. 在SQL中,如果使用逻辑运算符(例如=,!=,<等)在null值和任何其他值(包括另一个null)之间进行比较,则将导致null,这对于where子句。 The reasoning is that a null means "unknown", so the result of any comparison to a null is also "unknown". 原因是null表示“未知”,因此与null进行任何比较的结果也都是“未知”。 So you'll get no hit on rows using my_column = null. 因此,使用my_column = null不会对行产生任何影响。

SQL provides the special syntax for testing if a column is null, via is null and is not null, which is a special condition to test for a null (or not a null). SQL提供了特殊的语法来测试列是否为null,via是否为null以及是否为非null,这是测试null(或非null)的特殊条件。

 x is null checks whether x is a null value.
 x = null is checking whether x equals NULL, which will never be true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM