代码之家  ›  专栏  ›  技术社区  ›  Sandeepan Nath

无法将tinyint和varchar字段更新为默认值null和空字符串

  •  0
  • Sandeepan Nath  · 技术社区  · 5 年前

    CREATE TABLE `tablename` (
      `id` bigint(15) NOT NULL AUTO_INCREMENT,
      `uuid` varchar(400) NOT NULL,
      `pre_notif_action` varchar(30) DEFAULT '',
      `pre_notif_interval` tinyint(1) DEFAULT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uuid_UNIQUE` (`uuid`),
    
      ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    

    对于现有记录,分别在pre\u notif\u action和pre\u notif\u interval字段中使用值“predeactivate”和45-

    mysql> select pre_notif_action, pre_notif_interval 
           from tablename 
           where uuid="1887826113857166800";
    

    结果-

    +------------------+--------------------+
    | pre_notif_action | pre_notif_interval |
    +------------------+--------------------+
    | predeactivate    |                 45 |
    +------------------+--------------------+
    

    当我尝试编辑时,得到的是非零影响行-

     update prepaid_account 
     set pre_notif_action="" 
         and pre_notif_interval=NULL 
     where  uuid="1887826113857166800";
    

    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    

    但是,当我选择-

    mysql> select pre_notif_action, pre_notif_interval 
           from prepaid_account 
           where uuid="1887826113857166800";
    

    +------------------+--------------------+
    | pre_notif_action | pre_notif_interval |
    +------------------+--------------------+
    | 0                |                 45 |
    +------------------+--------------------+
    

    0 回复  |  直到 5 年前
        1
  •  1
  •   Rob Streeting David Hoerster    5 年前

    update prepaid_account 
     set pre_notif_action = ("" and pre_notif_interval=NULL)
     where  uuid="1887826113857166800";
    

    这个 ("" and pre_notif_interval=NULL) 被解释为布尔值,这就是为什么 0 是布尔的等价物 false 在MySQL中)。要解决此问题,请在SET子句的多个字段之间使用逗号,如下所示:

    update prepaid_account 
     set pre_notif_action = "", pre_notif_interval=NULL
     where  uuid="1887826113857166800";
    
        2
  •  1
  •   halfer    4 年前

    如果\u action=0作为结果或逻辑操作,则会得到预\u not:

    pre_notif_action="" 
     and pre_notif_interval=NULL
    

    pre_notif_action pre_notif_interval .

    更新的几列的正确语法是用逗号分隔的值,如下所示:

     update prepaid_account 
     set pre_notif_action="" 
         , pre_notif_interval=NULL 
     where  uuid="1887826113857166800";