MySQL插入选择 - 非空字段

时间:2013-12-19 01:24:58

标签: mysql sql

哦,嘿那里,

我试图通过INSERT ... SELECT语句将数据加载到表中,但是我遇到了MySQL处理NULL值的问题。

在下面的示例中,table1是源,table2是目标(请注意,table2对description字段有更多约束):

mysql> drop table if exists table1;
Query OK, 0 rows affected (0.03 sec)

mysql> drop table if exists table2;
Query OK, 0 rows affected (0.00 sec)

mysql> create table if not exists table1 (
    -> id int not null auto_increment,
    -> description varchar(45),
    -> primary key (`id`)
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql> create table if not exists table2 (
    -> id int not null auto_increment,
    -> description varchar(45) not null,
    -> primary key (`id`),
    -> unique index `unique_desc` (`description`)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert ignore into table1
    -> (description)
    -> values("stupid thing"),
    -> ("another thing"),
    -> (null),
    -> ("stupid thing"),
    -> ("last thing");
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from table1;
+----+---------------+
| id | description   |
+----+---------------+
|  1 | stupid thing  |
|  2 | another thing |
|  3 | NULL          |
|  4 | stupid thing  |
|  5 | last thing    |
+----+---------------+
5 rows in set (0.00 sec)

mysql> insert ignore into table2
    -> (description)
    -> select description
    -> from table1;
Query OK, 4 rows affected, 1 warning (0.01 sec)
Records: 5  Duplicates: 1  Warnings: 1

mysql> select * from table2;
+----+---------------+
| id | description   |
+----+---------------+
|  3 |               |
|  2 | another thing |
|  4 | last thing    |
|  1 | stupid thing  |
+----+---------------+
4 rows in set (0.00 sec)

空格和id = 3的行不应该在那里。我知道默认情况下MySQL以这种方式处理NOT NULL指令,但我尝试将sql_mode选项指定为“STRICT_ALL_TABLES”,我发现它具有以下影响:

没有设置sql_mode

mysql> drop table if exists table2;
Query OK, 0 rows affected (0.00 sec)

mysql> create table if not exists table2 (
    -> id int not null auto_increment,
    -> count int,
    -> description varchar(45) not null,
    -> primary key (`id`),
    -> unique index `unique_desc` (`description`)
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into table2
    -> (count,description)
    -> values(12,"stupid thing");
Query OK, 1 row affected (0.00 sec)

mysql> insert into table2
    -> (count)
    -> values(5);
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> select * from table2;
+----+-------+--------------+
| id | count | description  |
+----+-------+--------------+
|  1 |    12 | stupid thing |
|  2 |     5 |              |
+----+-------+--------------+
2 rows in set (0.00 sec)

sql_mode设置为“STRICT_ALL_TABLES”:

mysql> drop table if exists table1;
Query OK, 0 rows affected, 1 warning (0.03 sec)

mysql> drop table if exists table2;
Query OK, 0 rows affected (0.00 sec)

mysql> create table if not exists table2 (
    -> id int not null auto_increment,
    -> count int,
    -> description varchar(45) not null,
    -> primary key (`id`),
    -> unique index `unique_desc` (`description`)
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into table2
    -> (count,description)
    -> values(12,"stupid thing");
Query OK, 1 row affected (0.01 sec)

mysql> insert into table2
    -> (count)
    -> values(5);
ERROR 1364 (HY000): Field 'description' doesn't have a default value
mysql> select * from table2;
+----+-------+--------------+
| id | count | description  |
+----+-------+--------------+
|  1 |    12 | stupid thing |
+----+-------+--------------+
1 row in set (0.00 sec)

请注意,在上面的比较中,如果您明确地为description字段赋予NULL值,数据库将正确地抱怨并且没有设置“STRICT_ALL_TABLES”选项:

mysql> insert into table2
    -> (count,description)
    -> values(12,null);
ERROR 1048 (23000): Column 'description' cannot be null

结论:

由于某种原因,设置sql_mode会影响这种插入,但不会影响INSERT ... SELECT行为。

如何通过单个查询将table1中的数据导入table2,并且没有空单元格?

提前致谢,

ķ

1 个答案:

答案 0 :(得分:4)

只需使用WHERE子句:

insert ignore into table2(description)
select description from table1
where description <> '' and description is not null