mysqlデータベースでnullを使用すると問題が発生する可能性があります

7202 ワード

NOT IN、!= 等負条件クエリー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)
    
    
    -- 1NOT IN     NULLselect user_name from table_2 where user_name not in (select user_name from table_3 where id!=1)
    
    +-------------+
    | user_name   |
    |-------------|
    +-------------+
    
    
    -- 2nullnullnull,     “     ”    
    --   name   nullnull ,            。  ,   not nullselect * from table_3 where name != 'zhaoliu_2_1'
    
    Empty set (0.00 sec)
    
    
    -- 3、           :    +nullnullselect CONCAT("1",null) from dual; --      null-- 4Null column       ,count(Null column)      ,nullselect * 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 |
    +--------------------+
    
    -- 5Null= 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カラムにはより多くのストレージスペースが必要です