mysqlデータベースでnullを使用すると問題が発生する可能性があります
7202 ワード
NOT IN、!= 等負条件クエリーNULL値がある場合は常に空の結果を返し、クエリーがエラーになりやすい例
Nullカラムにはより多くのストレージスペースが必要です
create table table_2 (
`id` INT (11) NOT NULL,
user_name varchar(20) NOT NULL
)
create table table_3 (
`id` INT (11) NOT NULL,
user_name varchar(20)
)
insert into table_2 values (4,"zhaoliu_2_1"),(2,"lisi_2_1"),(3,"wangmazi_2_1"),(1,"zhangsan_2"),(2,"lisi_2_2"),(4,"zhaoliu_2_2"),(3,"wangmazi_2_2")
insert into table_3 values (1,"zhaoliu_2_1"),(2, null)
-- 1、NOT IN NULL ,
select user_name from table_2 where user_name not in (select user_name from table_3 where id!=1)
+-------------+
| user_name |
|-------------|
+-------------+
-- 2、 null , null , null, “ ”
-- name null, null , 。 , not null 。
select * from table_3 where name != 'zhaoliu_2_1'
Empty set (0.00 sec)
-- 3、 : + , null , null。
select CONCAT("1",null) from dual; -- null。
-- 4、 Null column ,count(Null column) ,null 。
select * from table_3;
+------+-------------+
| id | user_name |
|------+-------------|
| 1 | zhaoliu_2_1 |
| 2 | <null> |
| 21 | zhaoliu_2_1 |
| 22 | <null> |
+------+-------------+
4 rows in set
select count(user_name) from table_3;
+--------------------+
| count(user_name) |
|--------------------|
| 2 |
+--------------------+
-- 5、 Null , = null 。
create index IDX_test on table_3 (user_name);
select * from table_3 where user_name is null\G
select * from table_3 where user_name = null\G
desc select * from table_3 where user_name = 'zhaoliu_2_1'\G
desc select * from table_3 where user_name = null\G
desc select * from table_3 where user_name is null\G
Nullカラムにはより多くのストレージスペースが必要です