MySQL 插入选择 - NOT NULL 字段

标签 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 值,数据库将正确地提示 WITH AND WITHOUT WITH AND WITHOUT "STRICT_ALL_TABLES"option set:

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

结论:

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

如何通过单个查询将数据从 table1 获取到 table2,并且没有空单元格?

提前致谢

K

最佳答案

只需使用一个 WHERE 子句:

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

关于MySQL 插入选择 - NOT NULL 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20671853/

相关文章:

php - Laravel 4 - 从模型中返回平均分数?

mysql - SQL获取每分钟的数量

mysql - 一张表有多个外键

sql - 如何在 SQL 中将整数列值加 1

c# - 慢速 EF 查询按月/年分组数据

mysql - Zend 数据库 : How to find the actual amount of affected rows with INSERT ON DUPLICATE KEY?

php - fatal error : Uncaught PDOException: SQLSTATE[HY000]: General error: fetch mode requires the classname argument in

php - mySQL WordPress 在具有相同 ID 的单独行中进行过滤

MYSQL丢失用户密码

mysql - SQL连接多行