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

不打扰是莪最后的温柔 提交于 2020-04-11 12:42:47

问题


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. 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. 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). 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). It is not possible for an IS or IS NOT expression to evaluate to NULL. Operators IS and IS NOT have the same precedence as =.


I am confused about the keyword "IS" in SQLite.

I'm working on a project that requires me to use Java's prepared Statements. I've come across two types of WHERE clauses:

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"? Google searches show that most people use = for value comparison and IS for comparing to null. However I attempted a few SQLite queries of my own.

  • "IS" will return results as expected for "column IS NULL" and for "column IS value".
  • "=" will return results as expected for "column = value" but not for "column = NULL".

My question is can I use "IS" for both situations without unexpected results? I would like to make one prepared statement for a single query who's constraint on a column may or may not be 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":

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


回答1:


The "IS" keyword is strictly to be used with NULL value. (eg. IS NULL or 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.

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



回答2:


The above answers are getting it wrong. They're saying that '=' must be used in all cases and 'IS' for nulls. 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 on either side of = will evaluate to false. IS will evaluate the truth of the NULL condition.

This NULL = NULL will be false by the design of the operator and NULL is NULL will be 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' .




回答3:


in SQL language "IS" is used only when comparing with NULL (IS NULL or IS NOT NULL) since x = NULL will always fail (NULL is not equal to anything)




回答4:


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. you should use "Is null" or "Is not null". The reason is in SQL one null is not equals to another null. that means if you write something like null = null it will return you 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.

Refer to Codd's Rule 3: Systematic treatment of null values. It says one null is not equal to another null in relational database.

I am sharing this from 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"; 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. Similarly, when the question is, "Does Juanita own a car?", the answer "we don't know" is not the same thing as "no". The former yields a database entry of "null"; only the latter yields a database entry of "no".

Hope this will help !!




回答5:


If a column value is null, the = operator will never return true when comparing with null, so NULL != NULL, hence the need to test with IS NULL.




回答6:


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. The reasoning is that a null means "unknown", so the result of any comparison to a null is also "unknown". So you'll get no hit on rows using 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).

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


来源:https://stackoverflow.com/questions/15233870/sqlite-difference-between-is-and-equals-in-where-clause-using-jdbc-prepa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!