问题:

使用游标遍历时,发现使用

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 storesWHERE EXISTS (SELECT * FROM cities_storesWHERE cities_stores.store_type = stores.store_type);
What kind of store is present in no cities?SELECT DISTINCT store_type FROM storesWHERE NOT EXISTS (SELECT * FROM cities_storesWHERE cities_stores.store_type = stores.store_type);
What kind of store is present in all cities?SELECT DISTINCT store_type FROM stores s1WHERE NOT EXISTS (SELECT * FROM cities WHERE NOT EXISTS (SELECT * FROM cities_storesWHERE cities_stores.city = cities.cityAND 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/

转载于:https://www.cnblogs.com/davidwang456/p/3968026.html

mysql 变量is null 和 not exists区别相关推荐

  1. MySQL 中NULL和空值的区别

    平时我们在使用MySQL的时候,对于MySQL中的NULL值和空值区别不能很好的理解.注意到NULL值是未知的,且占用空间,不走索引,DBA建议建表的时候最好设置字段是NOT NULL 来避免这种低效 ...

  2. mysql declare 赋值_sql server和mysql变量赋值的区别 以及 MySql Declare(转)

    sql server和mysql都是我们经常用到的数据库系统,下面就为您介绍sql server和mysql变量赋值的区别,希望对您能有所启迪. sql server中变量要先申明后赋值: 局部变量用 ...

  3. [JS] undefined、null、ReferenceError的区别、变量作用域问题

    undefined.null.ReferenceError的区别 null表示"没有对象",即该处不应该有值. 典型用法是: (1) 作为函数的参数,表示该函数的参数不是对象. ( ...

  4. mysql 的not null 与 null的区别(转,恍然大悟)

    相信很多用了mysql很久的人,对这两个字段属性的概念还不是很清楚,一般会有以下疑问: 1.我字段类型是not null,为什么我可以插入空值 2.为毛not null的效率比null高 3.判断字段 ...

  5. mysql两个字段相减_MySQL 中NULL和空值的区别?

    作为后台开发,在日常工作中如果要接触Mysql数据库,那么不可避免会遇到Mysql中的NULL和空值.那你知道它们有什么区别吗? 学不动了,也不想知道它们有什么区别.大兄弟,不行啊,要面试! 前些天我 ...

  6. mysql declare 赋值_sql server和mysql变量赋值的区别 以及 MySql Declare

    sql server和mysql都是我们经常用到的数据库系统,下面就为您介绍sql server和mysql变量赋值的区别,希望对您能有所启迪. sql server中变量要先申明后赋值: 局部变量用 ...

  7. MySQL null与not null和null与空值‘‘的区别

    null 表示什么也不是, 不能=.>.< - 所有的判断,结果都是false,所有只能用 is null进行判断. 转自:https://segmentfault.com/a/11900 ...

  8. mysql null 0 空_MySQL中 null与not null和null与空值''的区别

    相信很多用了MySQL很久的人,对这两个字段属性的概念还不是很清楚,一般会有以下疑问: 1.字段类型是not null,为什么可以插入空值? 2.为什么not null的效率比null高? 3.判断字 ...

  9. 第一讲javascript入门(js的组成、事件类型、嵌入方式、注释方式、输出方式、变量类型 、变量命名规则、字符串转义符、算术运算符、数据类型转换、null和undefined的区别)

    目录 理论: JavaScript是什么 js的组成 二.JavaScript引入方式 1.嵌入式 2.外部引入 事件定义 三.JavaScript注释方式 1.单行注释 2.多行注释 四.JavaS ...

最新文章

  1. Android Studio报错解决:droid.tools.idea.welcome.install.WizardException: SDK tools directory is missing
  2. 关于mysql中truncate
  3. 汇编语言--iret指令
  4. C语言构建一个链表以及操作链表
  5. JSLint JavaScript代码质量审查工具汉化中文版隆重发布
  6. html5画折线图,canvas绘制折线图(仿echarts)
  7. selenium与python自动化测试模拟登录百度
  8. Python 打印嵌套list中每个数据(遍历列表)
  9. Akash Network主网现已部署Sushiswap应用
  10. Lightroom 教程,如何将照片从 Lightroom 移至Photoshop,在 Ps 中合并图像?
  11. (Abstract Factory)抽象工厂模式的Java实现
  12. stm32程序跑飞_mm32芯片使用心得(三)音频播放程序修改
  13. 数字图像处理与机器视觉,机器视觉算法与应用 pdf电子版
  14. 常用的图像标注工具汇总
  15. 2022年R2移动式压力容器充装上岗证题库及在线模拟考试
  16. APP测试点(思维导图)
  17. pytorch中dim的含义及相关做法
  18. puts 和 printf %s
  19. 腾讯微信客服电话号码是多少
  20. 计算机无法关闭密码保护共享,xp系统怎么关闭密码保护共享

热门文章

  1. python如何下载tushare_安装tushare
  2. 未来新一代计算机的发展方向,未来计算机的发展方向 (2)
  3. android 控件监听方法,Android界面控件(2)—注册点击事件监听器
  4. python难度如何_入门Python学习难吗怎样规划学习路线
  5. c语言文件查找函数fread,文件函数fread
  6. android gridview滑动卡,Android RecyclerView的卡顿问题
  7. python3是unicode还是utf-8_ASCII、Unicode、UTF-8以及Python3编码问题
  8. 圆弧与直线相切画法_数控编程基础,相切圆弧的基点计算方法
  9. visual studio输入法打不了中文_目前比较满意的手机输入法方案:Gboard + 搜狗词库...
  10. 设计一个有getMin功能的栈 (python)