Processing when Oracle's where condition in/not in contains NULL

=3) It means that our previous guess is correct 2. The in condition contains NULL linuxidc@TEST>select * from t_in where id in (1,3,null); ID ---------- 1 3 2 rows selected. The above conditions are equivalent to id = 1 or id = 3 or id = null, let's look at http://www.cppentry.com Programming Development Programmers Getting Started The following figure shows Oracle when there is an id = null condition How to handle It can be seen from the above figure that when the id value is NULL or non-NULL, the result of id = NULL is UNKNOWN, which is also equivalent to FALSE. Therefore, only two records 1 and 3 were found in the above search results. Check the execution plan to see the rewrite of IN by the optimizer 3. The not in condition does not contain NULL values ​​linuxidc@TEST>select * from t_in where id not in (1,3); ID -------- -- 2 4 2 rows selected. The where condition of the above query is equivalent to id != 1 and id !=3. In addition, there is a row in the t_in table that is null. Although it satisfies !=1 and !=3, according to the above rules, The result of = or != comparison between NULL and other values ​​is UNKNOWN, so only 2 and 4 are found. See the rewrite of IN by the optimizer from the execution plan 4. The not in condition contains NULL values ​​linuxidc@TEST>select * from t_in where id not in (1,3,null); no rows selected where in the above query The condition is equivalent to id!=1 and id!=3 and id!=null, according to the above rules, NULL is =or with other values! = The comparison results are UNKNOWN, so the entire condition is equivalent to FALSE, and no data is found in the end. View the rewrite of IN by the optimizer from the execution plan. To sum up, when using in as a condition, you can never find a row with a NULL value in the target column. If the not in condition contains a null value, it will not return any results, including in Contains subqueries. Therefore, in actual work, we must pay attention to whether the subquery contained in not in contains null values. linuxidc@TEST>select * from t_in where id not in (select id from t_in where id = 1 or id is null); no rows selected Official documentation: http://docs.oracle.com/cd/E11882_01/server.112 /e41084/sql_elements005.htm#SQLRF51096 http://docs.oracle.com/cd/E11882_01/server.112/e41084/conditions013.htm#SQLRF52169 http://docs.oracle.com/cd/E11882_01/server.112 /e41084/conditions004.htm#SQLRF52116

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326343843&siteId=291194637