繁体   English   中英

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

[英]unable to update to default values null and empty string for tinyint and varchar fields

我有以下表格架构 -

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_notif_action 和 pre_notif_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 |
+------------------+--------------------+

我该如何解决这个问题?

你得到 pre_notif_action = 0 作为结果或逻辑运算:

pre_notif_action="" 
 and pre_notif_interval=NULL

返回 0 (false)

因此,您使用的是逻辑运算,而不是更新两列pre_notif_actionpre_notif_interval

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

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

我相信这里的问题是在 SET 子句中使用了 AND。 我认为您的查询是这样解释的:

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

("" and pre_notif_interval=NULL)被解释为布尔值,这就是为什么将0插入到该字段中的原因( 0相当于 MySQL 中的 boolean false )。 要解决此问题,请在 SET 子句中的多个字段之间使用逗号,如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM