mysql 变量is null 和 not exists区别
问题:
使用游标遍历时,发现使用
select var into tmp where var=?
然后判断if tmp is null时,不能走完所有的遍历。经debug发现,
当var为空时,则跳出游标的遍历。
解决方式:
使用if not exists(select var into tmp where var=?)时,则ok。
这个可以从mysql官方文档中找到原因:
1. select var into tmp where var=? 中where 条件不支持为空,如下面红色部分所示。
- Problems with NULL Values
- The concept of the NULL value is a common source of confusion for newcomers to SQL, who often think that NULL is the same thing as an empty string ''. This is not the case. For example, the following statements are completely different:
- mysql> INSERT INTO my_table (phone) VALUES (NULL);
- mysql> INSERT INTO my_table (phone) VALUES ('');
- Both statements insert a value into the phone column, but the first inserts a NULL value and the second inserts an empty string. The meaning of the first can be regarded as “phone number is not known” and the meaning of the second can be regarded as “the person is known to have no phone, and thus no phone number.”
- To help with NULL handling, you can use the IS NULL and IS NOT NULL operators and the IFNULL() function.
- In SQL, the NULL value is never true in comparison to any other value, even NULL. An expression that contains NULL always produces a NULL value unless otherwise indicated in the documentation for the operators and functions involved in the expression. All columns in the following example return NULL:
- mysql> SELECT NULL, 1+NULL, CONCAT('Invisible',NULL);
- To search for column values that are NULL, you cannot use an expr = NULL test. The following statement returns no rows, because expr = NULL is never true for any expression:
- mysql> SELECT * FROM my_table WHERE phone = NULL;
- To look for NULL values, you must use the IS NULL test. The following statements show how to find the NULL phone number and the empty phone number:
- mysql> SELECT * FROM my_table WHERE phone IS NULL;
- mysql> SELECT * FROM my_table WHERE phone = '';
- See Section 3.3.4.6, “Working with NULL Values”, for additional information and examples.
- You can add an index on a column that can have NULL values if you are using the MyISAM, InnoDB, or BDB, or MEMORY storage engine. Otherwise, you must declare an indexed column NOT NULL, and you cannot insert NULL into the column.
- When reading data with LOAD DATA INFILE, empty or missing columns are updated with ''. To load a NULL value into a column, use \N in the data file. The literal word “NULL” may also be used under some circumstances. See Section 13.2.6, “LOAD DATA INFILE Syntax”.
- When using DISTINCT, GROUP BY, or ORDER BY, all NULL values are regarded as equal.
- When using ORDER BY, NULL values are presented first, or last if you specify DESC to sort in descending order.
- Aggregate (summary) functions such as COUNT(), MIN(), and SUM() ignore NULL values. The exception to this is COUNT(*), which counts rows and not individual column values. For example, the following statement produces two counts. The first is a count of the number of rows in the table, and the second is a count of the number of non-NULL values in the age column:
- mysql> SELECT COUNT(*), COUNT(age) FROM person;
- For some data types, MySQL handles NULL values specially. If you insert NULL into a TIMESTAMP column, the current date and time is inserted. If you insert NULL into an integer or floating-point column that has the AUTO_INCREMENT attribute, the next number in the sequence is inserted.
2. 从下面红色部分可以得到 exisit 判断记录是否存在,不管select colum等同于select * ,mysql会忽略select colum的列而且允许有null 行。
- Subqueries with EXISTS or NOT EXISTS
- If a subquery returns any rows at all, EXISTS subquery is TRUE, and NOT EXISTS subquery is FALSE. For example:
- SELECT column1 FROM t1 WHERE EXISTS (SELECT * FROM t2);
- Traditionally, an EXISTS subquery starts with SELECT *, but it could begin with SELECT 5 or SELECT column1 or anything at all. MySQL ignores the SELECT list in such a subquery, so it makes no difference.
- For the preceding example, if t2 contains any rows, even rows with nothing but NULL values, the EXISTS condition is TRUE. This is actually an unlikely example because a [NOT] EXISTS subquery almost always contains correlations. Here are some more realistic examples:
- What kind of store is present in one or more cities?
- SELECT DISTINCT store_type FROM stores
- WHERE EXISTS (SELECT * FROM cities_stores
- WHERE cities_stores.store_type = stores.store_type);
- What kind of store is present in no cities?
- SELECT DISTINCT store_type FROM stores
- WHERE NOT EXISTS (SELECT * FROM cities_stores
- WHERE cities_stores.store_type = stores.store_type);
- What kind of store is present in all cities?
- SELECT DISTINCT store_type FROM stores s1
- WHERE NOT EXISTS (
- SELECT * FROM cities WHERE NOT EXISTS (
- SELECT * FROM cities_stores
- WHERE cities_stores.city = cities.city
- AND cities_stores.store_type = stores.store_type));
- The last example is a double-nested NOT EXISTS query. That is, it has a NOT EXISTS clause within a NOT EXISTS clause. Formally, it answers the question “does a city exist with a store that is not in Stores”? But it is easier to say that a nested NOT EXISTS answers the question “is x TRUE for all y?”
The Apache Mahout™ project's goal is to build a scalable machine learning library.
https://mahout.apache.org/
mysql 变量is null 和 not exists区别的更多相关文章
- MySQL 中NULL和空值的区别
平时我们在使用MySQL的时候,对于MySQL中的NULL值和空值区别不能很好的理解.注意到NULL值是未知的,且占用空间,不走索引,DBA建议建表的时候最好设置字段是NOT NULL 来避免这种低效 ...
- MySQL 中NULL和空值的区别 (转载 http://blog.sina.com.cn/s/blog_3f2a82610102v4dn.html)
平时我们在使用MySQL的时候,对于MySQL中的NULL值和空值区别不能很好的理解.注意到NULL值是未知的,且占用空间,不走索引,DBA建议建表的时候最好设置字段是NOT NULL 来避免这种低效 ...
- mysql not in、left join、IS NULL、NOT EXISTS 效率问题记录
原文:mysql not in.left join.IS NULL.NOT EXISTS 效率问题记录 mysql not in.left join.IS NULL.NOT EXISTS 效率问题记录 ...
- mysql 的not null 与 null的区别(转,恍然大悟)
相信很多用了mysql很久的人,对这两个字段属性的概念还不是很清楚,一般会有以下疑问: 1.我字段类型是not null,为什么我可以插入空值 2.为毛not null的效率比null高 3.判断字段 ...
- MySQL null与not null和null与空值''的区别
参考连接:https://segmentfault.com/a/1190000009540449 相信很多用了MySQL很久的人,对这两个字段属性的概念还不是很清楚,一般会有以下疑问: 我字段类型是n ...
- MySQL 中NULL和空值的区别,索引列可以有空值或者null吗?
空值跟null的区别.mysql官方: “NULL columns require additional space in the row to record whether their values ...
- 【面试】MySQL 中NULL和空值的区别?
做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 01 小木的故事 作为后台开发,在日常工作中如果要接触Mysql数据库,那么不可避免会遇到Mysql中的NULL和空值.那 ...
- MySql中的count、NULL和空串的区别
**1.count (1).count (*) 与 count (列名) 的区别** 表 count(1) count(*) count (列名) 作用 统计表中的所有的记录数 会统计表中的所有的记录 ...
- mysql 空值(null)和空字符('')的区别
日常开发中,一般都会涉及到数据库增删改查,那么不可避免会遇到Mysql中的NULL和空字符. 空字符(")和空值(null)表面上看都是空,其实存在一些差异: 定义: 空值(NULL)的长度 ...
随机推荐
- 关于idea激活
1.在线激活 https://www.iteblog.com/idea/ 使用可访问:http://idea.iteblog.com/ 或者 http://idea.lanyus.com/ ht ...
- Hibernate 性能优化之查询缓存
查询缓存是建立在二级缓存基础之上的,所以与二级缓存特性相似,是共享的,适合修改不是很频繁的数据 查询缓存不是默认开启的,需要设置 1.在cfg文件中配置 <property name= ...
- Hibernate 环境搭建
Hibernate 工作流程 1.创建工程并导包 2.在src根目录下创建配置文件:hibernate.cfg.xml(也可以创建在src其他文件夹下,但是在后面的配置中,需要指明路径) <?x ...
- linux 进程数
一.linux系统支持的最大进程数 限制1:既然系统使用pid_t表示进程号,那么最大进程数不能超过pid_t类型的最大值吧 限制2:使用命令ulimit -u查看系统中限制的最大进程数,我的机器上是 ...
- grails&groovy的IllegalArgument异常
我在开发的过程中遇到了这样一个异常,总是提示IllegalArgument异常,代码大致如下: if(haomgl.save(flush:true)){ //更新库存:状态为2的位置存煤 def cu ...
- struts1、ajax、jquery、json简单实例
1.页面ajax代码,使用$.ajax,获得json对象后each $.ajax({ type:"GET", url:ctx + "/uploadImg.do" ...
- Hibernate的查询语言之HQL(一)——快速入门
Hibernate提供异常强大的查询体系,使用Hibernat有多种查询方式可以选择:即可以使用Hibernate的HQL查询,也可以使用条件查询,甚至可以使用原生的SQL查询语句.不仅如此, Hib ...
- 自然语言处理(1)之NLTK与PYTHON
自然语言处理(1)之NLTK与PYTHON 题记: 由于现在的项目是搜索引擎,所以不由的对自然语言处理产生了好奇,再加上一直以来都想学Python,只是没有机会与时间.碰巧这几天在亚马逊上找书时发现了 ...
- 【Maven实战】依赖的范围
在Maven中有三大模块,分别是依赖.仓库.生命周期和插件,我们接下来下来介绍下依赖,为了方便起见我们还是以案例来说: 1.首先建立一个maven项目,这里我建立一个user的项目 2.接下来我们在这 ...
- Ubuntu 启用root账户
设置分配很简单,只要为root设置一个root密码就行了: $ sudo passwd root 之后会提示要输入root用户的密码,连续输入root密码,再使用:$ su 就可以切换成超级管理员用户 ...