加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

The Apache Mahout™ project's goal is to bu

发布时间:2020-12-11 23:55:43 所属栏目:MySql教程 来源:网络整理
导读:问题: 使用游标遍历时,发现使用 select var into tmp where var=? 然后判断if tmp is null时,不能走完所有的遍历。经debug发现, 当var为空时,则跳出游标的遍历。 解决方式: 使用if not exists(select var into tmp where var=?)时,则ok。 这个可以从m

问题:

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

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 the value a common source confusion newcomers SQL,who often think that the same thing an empty string . This the . mysql<span style="color: #808080;">> <span style="color: #0000ff;">INSERT <span style="color: #0000ff;">INTO my_table (phone) <span style="color: #0000ff;">VALUES (<span style="color: #0000ff;">NULL<span style="color: #000000;">);
mysql
<span style="color: #808080;">>
<span style="color: #0000ff;">INSERT
<span style="color: #0000ff;">INTO
my_table (phone) <span style="color: #0000ff;">VALUES
(<span style="color: #ff0000;">''
<span style="color: #000000;">);
Both statements
<span style="color: #0000ff;">insert
a value <span style="color: #0000ff;">into
the phone <span style="color: #0000ff;">column
,but the first inserts a <span style="color: #0000ff;">NULL value <span style="color: #808080;">and the second inserts an empty string. The meaning <span style="color: #0000ff;">of the first can be regarded <span style="color: #0000ff;">as “phone <span style="color: #0000ff;">number <span style="color: #0000ff;">is <span style="color: #808080;">not known” <span style="color: #808080;">and the meaning <span style="color: #0000ff;">of the second can be regarded <span style="color: #0000ff;">as “the person <span style="color: #0000ff;">is known <span style="color: #0000ff;">to have no phone,<span style="color: #808080;">and thus no phone <span style="color: #0000ff;">number<span style="color: #000000;">.”

<span style="color: #0000ff;">To help <span style="color: #0000ff;">with <span style="color: #0000ff;">NULL handling,you can <span style="color: #0000ff;">use the <span style="color: #0000ff;">IS <span style="color: #0000ff;">NULL <span style="color: #808080;">and <span style="color: #0000ff;">IS <span style="color: #808080;">NOT <span style="color: #0000ff;">NULL operators <span style="color: #808080;">and the IFNULL() <span style="color: #0000ff;">function<span style="color: #000000;">.

<span style="color: #808080;">In SQL,the <span style="color: #0000ff;">NULL value <span style="color: #0000ff;">is never true <span style="color: #808080;">in comparison <span style="color: #0000ff;">to <span style="color: #808080;">any other value,even <span style="color: #0000ff;">NULL. An expression that <span style="color: #0000ff;">contains <span style="color: #0000ff;">NULL always produces a <span style="color: #0000ff;">NULL value unless otherwise indicated <span style="color: #808080;">in the documentation <span style="color: #0000ff;">for the operators <span style="color: #808080;">and functions involved <span style="color: #808080;">in the expression. <span style="color: #808080;">All columns <span style="color: #808080;">in the following example <span style="color: #0000ff;">return <span style="color: #0000ff;">NULL<span style="color: #000000;">:

mysql<span style="color: #808080;">> <span style="color: #0000ff;">SELECT <span style="color: #0000ff;">NULL,<span style="color: #800000; font-weight: bold;">1<span style="color: #808080;">+<span style="color: #0000ff;">NULL,CONCAT(<span style="color: #ff0000;">'<span style="color: #ff0000;">Invisible<span style="color: #ff0000;">',<span style="color: #0000ff;">NULL<span style="color: #000000;">);
<span style="color: #ff0000;">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;
<span style="color: #0000ff;">To look <span style="color: #0000ff;">for <span style="color: #0000ff;">NULL <span style="color: #0000ff;">values,you must <span style="color: #0000ff;">use the <span style="color: #0000ff;">IS <span style="color: #0000ff;">NULL test. The following statements show how <span style="color: #0000ff;">to find the <span style="color: #0000ff;">NULL phone <span style="color: #0000ff;">number <span style="color: #808080;">and the empty phone <span style="color: #0000ff;">number<span style="color: #000000;">:

mysql<span style="color: #808080;">> <span style="color: #0000ff;">SELECT <span style="color: #808080;"> <span style="color: #0000ff;">FROM my_table <span style="color: #0000ff;">WHERE phone <span style="color: #0000ff;">IS <span style="color: #0000ff;">NULL<span style="color: #000000;">;
mysql<span style="color: #808080;">> <span style="color: #0000ff;">SELECT <span style="color: #808080;">
<span style="color: #0000ff;">FROM my_table <span style="color: #0000ff;">WHERE phone <span style="color: #808080;">= <span style="color: #ff0000;">''<span style="color: #000000;">;
See Section <span style="color: #800000; font-weight: bold;">3.3.<span style="color: #800000; font-weight: bold;">4.6,“Working <span style="color: #0000ff;">with <span style="color: #0000ff;">NULL <span style="color: #0000ff;">Values”,<span style="color: #0000ff;">for additional information <span style="color: #808080;">and<span style="color: #000000;"> examples.

You can <span style="color: #0000ff;">add an <span style="color: #0000ff;">index <span style="color: #0000ff;">on a <span style="color: #0000ff;">column that can have <span style="color: #0000ff;">NULL <span style="color: #0000ff;">values <span style="color: #0000ff;">if you are using the MyISAM,InnoDB,<span style="color: #808080;">or BDB,<span style="color: #808080;">or MEMORY storage engine. Otherwise,you must <span style="color: #0000ff;">declare an indexed <span style="color: #0000ff;">column <span style="color: #808080;">NOT <span style="color: #0000ff;">NULL,<span style="color: #808080;">and you cannot <span style="color: #0000ff;">insert <span style="color: #0000ff;">NULL <span style="color: #0000ff;">into the <span style="color: #0000ff;">column<span style="color: #000000;">.

<span style="color: #0000ff;">When reading data <span style="color: #0000ff;">with <span style="color: #0000ff;">LOAD DATA INFILE,empty <span style="color: #808080;">or missing columns are updated <span style="color: #0000ff;">with <span style="color: #ff0000;">''. <span style="color: #0000ff;">To <span style="color: #0000ff;">load a <span style="color: #0000ff;">NULL value <span style="color: #0000ff;">into a <span style="color: #0000ff;">column,<span style="color: #0000ff;">use N <span style="color: #808080;">in the data <span style="color: #0000ff;">file. The literal word “<span style="color: #0000ff;">NULL” may also be used under <span style="color: #808080;">some circumstances. See Section <span style="color: #800000; font-weight: bold;">13.2.<span style="color: #800000; font-weight: bold;">6,“<span style="color: #0000ff;">LOAD<span style="color: #000000;"> DATA INFILE Syntax”.

<span style="color: #0000ff;">When using <span style="color: #0000ff;">DISTINCT,<span style="color: #0000ff;">GROUP <span style="color: #0000ff;">BY,<span style="color: #808080;">or <span style="color: #0000ff;">ORDER <span style="color: #0000ff;">BY,<span style="color: #808080;">all <span style="color: #0000ff;">NULL <span style="color: #0000ff;">values are regarded <span style="color: #0000ff;">as<span style="color: #000000;"> equal.

<span style="color: #0000ff;">When using <span style="color: #0000ff;">ORDER <span style="color: #0000ff;">BY,<span style="color: #0000ff;">NULL <span style="color: #0000ff;">values are presented first,<span style="color: #808080;">or last <span style="color: #0000ff;">if you specify <span style="color: #0000ff;">DESC <span style="color: #0000ff;">to sort <span style="color: #808080;">in descending <span style="color: #0000ff;">order<span style="color: #000000;">.

Aggregate (summary) functions such <span style="color: #0000ff;">as <span style="color: #ff00ff;">COUNT(),<span style="color: #ff00ff;">MIN(),<span style="color: #808080;">and <span style="color: #ff00ff;">SUM() ignore <span style="color: #0000ff;">NULL <span style="color: #0000ff;">values. The exception <span style="color: #0000ff;">to this <span style="color: #0000ff;">is <span style="color: #ff00ff;">COUNT(<span style="color: #808080;">*),which counts rows <span style="color: #808080;">and <span style="color: #808080;">not individual <span style="color: #0000ff;">column <span style="color: #0000ff;">values. <span style="color: #0000ff;">For example,the following statement produces two counts. The first <span style="color: #0000ff;">is a <span style="color: #ff00ff;">count <span style="color: #0000ff;">of the <span style="color: #0000ff;">number <span style="color: #0000ff;">of rows <span style="color: #808080;">in the <span style="color: #0000ff;">table,<span style="color: #808080;">and the second <span style="color: #0000ff;">is a <span style="color: #ff00ff;">count <span style="color: #0000ff;">of the <span style="color: #0000ff;">number <span style="color: #0000ff;">of non<span style="color: #808080;">-<span style="color: #0000ff;">NULL <span style="color: #0000ff;">values <span style="color: #808080;">in the age <span style="color: #0000ff;">column<span style="color: #000000;">:

mysql<span style="color: #808080;">> <span style="color: #0000ff;">SELECT <span style="color: #ff00ff;">COUNT(<span style="color: #808080;">*),<span style="color: #ff00ff;">COUNT(age) <span style="color: #0000ff;">FROM<span style="color: #000000;"> person;
<span style="color: #0000ff;">For <span style="color: #808080;">some data types,MySQL handles <span style="color: #0000ff;">NULL <span style="color: #0000ff;">values specially. <span style="color: #0000ff;">If you <span style="color: #0000ff;">insert <span style="color: #0000ff;">NULL <span style="color: #0000ff;">into a <span style="color: #0000ff;">TIMESTAMP <span style="color: #0000ff;">column,the <span style="color: #0000ff;">current date <span style="color: #808080;">and time <span style="color: #0000ff;">is inserted. <span style="color: #0000ff;">If you <span style="color: #0000ff;">insert <span style="color: #0000ff;">NULL <span style="color: #0000ff;">into an <span style="color: #0000ff;">integer <span style="color: #808080;">or floating<span style="color: #808080;">-point <span style="color: #0000ff;">column that has the AUTO_INCREMENT attribute,the <span style="color: #0000ff;">next <span style="color: #0000ff;">number <span style="color: #808080;">in the sequence <span style="color: #0000ff;">is inserted.

2. 从下面红色部分可以得到 exisit 判断记录是否存在,不管select colum等同于select * ,mysql会忽略select colum的列而且允许有null 行。

Subqueries a subquery rows at , subquery TRUE, subquery FALSE. <span style="color: #0000ff;">SELECT column1 <span style="color: #0000ff;">FROM t1 <span style="color: #0000ff;">WHERE <span style="color: #808080;">EXISTS (<span style="color: #0000ff;">SELECT <span style="color: #808080;"> <span style="color: #0000ff;">FROM<span style="color: #000000;"> t2);
<span style="color: #ff0000;">Traditionally,an
<span style="color: #ff0000;">EXISTS subquery starts with SELECT ,but it could begin with SELECT <span style="font-weight: bold;">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 <span style="color: #0000ff;">is actually an unlikely example because a <span style="color: #ff0000;">[<span style="color: #ff0000;">NOT<span style="color: #ff0000;">] <span style="color: #808080;">EXISTS subquery almost always <span style="color: #0000ff;">contains correlations. Here are <span style="color: #808080;">some<span style="color: #000000;"> more realistic examples:

What kind <span style="color: #0000ff;">of store <span style="color: #0000ff;">is present <span style="color: #808080;">in one <span style="color: #808080;">or<span style="color: #000000;"> more cities?

<span style="color: #0000ff;">SELECT <span style="color: #0000ff;">DISTINCT store_type <span style="color: #0000ff;">FROM<span style="color: #000000;"> stores
<span style="color: #0000ff;">WHERE <span style="color: #808080;">EXISTS (<span style="color: #0000ff;">SELECT <span style="color: #808080;">* <span style="color: #0000ff;">FROM<span style="color: #000000;"> cities_stores
<span style="color: #0000ff;">WHERE cities_stores.store_type <span style="color: #808080;">=<span style="color: #000000;"> stores.store_type);
What kind <span style="color: #0000ff;">of store <span style="color: #0000ff;">is present <span style="color: #808080;">in<span style="color: #000000;"> no cities?

<span style="color: #0000ff;">SELECT <span style="color: #0000ff;">DISTINCT store_type <span style="color: #0000ff;">FROM<span style="color: #000000;"> stores
<span style="color: #0000ff;">WHERE <span style="color: #808080;">NOT <span style="color: #808080;">EXISTS (<span style="color: #0000ff;">SELECT <span style="color: #808080;">* <span style="color: #0000ff;">FROM<span style="color: #000000;"> cities_stores
<span style="color: #0000ff;">WHERE cities_stores.store_type <span style="color: #808080;">=<span style="color: #000000;"> stores.store_type);
What kind <span style="color: #0000ff;">of store <span style="color: #0000ff;">is present <span style="color: #808080;">in <span style="color: #808080;">all<span style="color: #000000;"> cities?

<span style="color: #0000ff;">SELECT <span style="color: #0000ff;">DISTINCT store_type <span style="color: #0000ff;">FROM<span style="color: #000000;"> stores s1
<span style="color: #0000ff;">WHERE <span style="color: #808080;">NOT <span style="color: #808080;">EXISTS<span style="color: #000000;"> (
<span style="color: #0000ff;">SELECT <span style="color: #808080;"> <span style="color: #0000ff;">FROM cities <span style="color: #0000ff;">WHERE <span style="color: #808080;">NOT <span style="color: #808080;">EXISTS<span style="color: #000000;"> (
<span style="color: #0000ff;">SELECT <span style="color: #808080;">
<span style="color: #0000ff;">FROM<span style="color: #000000;"> cities_stores
<span style="color: #0000ff;">WHERE cities_stores.city <span style="color: #808080;">=<span style="color: #000000;"> cities.city
<span style="color: #808080;">AND cities_stores.store_type <span style="color: #808080;">=<span style="color: #000000;"> stores.store_type));
The last example <span style="color: #0000ff;">is a <span style="color: #0000ff;">double<span style="color: #808080;">-nested <span style="color: #808080;">NOT <span style="color: #808080;">EXISTS query. That <span style="color: #0000ff;">is,it has a <span style="color: #808080;">NOT <span style="color: #808080;">EXISTS clause within a <span style="color: #808080;">NOT <span style="color: #808080;">EXISTS clause. Formally,it answers the question “does a city exist <span style="color: #0000ff;">with a store that <span style="color: #0000ff;">is <span style="color: #808080;">not <span style="color: #808080;">in Stores”? But it <span style="color: #0000ff;">is easier <span style="color: #0000ff;">to say that a nested <span style="color: #808080;">NOT <span style="color: #808080;">EXISTS answers the question “<span style="color: #0000ff;">is x TRUE <span style="color: #0000ff;">for <span style="color: #808080;">all y?”

The Apache Mahout? project's goal is to build a scalable machine learning library.

https://mahout.apache.org/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读