【问题标题】:unable to update to default values null and empty string for tinyint and varchar fields无法为 tinyint 和 varchar 字段更新为默认值 null 和空字符串
【发布时间】:2020-01-07 05:43:02
【问题描述】:

我有以下表格架构 -

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 |
+------------------+--------------------+

我该如何解决这个问题?

【问题讨论】:

  • @MadhurBhaiya 有一些更新正在发生。这不像是改变根本不适用。
  • 相同的结果。为什么要问单引号?
  • 如果要更改多个列,请使用逗号代替 AND 子句中的 AND。是的,您应该对字符串文字使用单引号。

标签: mysql varchar tinyint


【解决方案1】:

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

pre_notif_action="" 
 and pre_notif_interval=NULL

返回 0(假)

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

更新的几列的正确语法是用逗号分隔的值:

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

【讨论】:

    【解决方案2】:

    我认为这里的问题是在 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 中的布尔值 false)。要解决此问题,请在 SET 子句中的多个字段之间使用逗号,如下所示:

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

    【讨论】:

      猜你喜欢
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      相关资源
      最近更新 更多