MariaDB 错误 1292:“截断了不正确的 DECIMAL 值”

问题描述 投票:0回答:1

使用不应使用任何十进制类型的 SQL 语句更新不包含任何十进制字段的 MariaDB InnoDB 表时,出现错误 1292“截断了不正确的 DECIMAL 值”。

这是表格,即将更新:

CREATE TABLE `nameinfo` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `parent` int(10) unsigned DEFAULT NULL,
  `related` int(10) unsigned DEFAULT NULL,
  `created` int(10) unsigned DEFAULT NULL,
  `name` varchar(256) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `parentRelatedNameIndex` (`parent`,`related`,`name`),
  KEY `parentIndex` (`parent`),
  KEY `relatedIndex` (`related`),
  KEY `createdIndex` (`created`),
  KEY `nameIndex` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;

该表包含值为

x-NAME-1
的记录,必须将其更改为
NAME
x-
必须从开头截掉,
-1
必须从结尾截掉)。还有一些更多的限制 - 但这是主要任务。

这是SQL语句:

UPDATE `nameinfo` AS plu SET 
  plu.`name`=MID(plu.`name`,3,LENGTH(plu.`name`)-4) 
WHERE 
  LENGTH(plu.`name`)>4 AND 
  LEFT(plu.`name`,2)='x-' AND 
  RIGHT(plu.`name`,2)='-1' AND 
  NOT EXISTS (
      SELECT u.* FROM `nameinfo` AS u WHERE 
      u.`parent`=plu.`parent` AND 
      u.`related`=plu.`related` AND 
      u.`name`=plu.`name`=MID(plu.`name`,3,LENGTH(plu.`name`)-4) 
      LIMIT 0,1
  );

那么错误是

Error Code: 1292
Truncated incorrect DECIMAL value: 'ZIMMR'

将记录从

x-ZIMMR-1
更新为
ZIMMR
时。

有人知道这是怎么回事吗?我看不到任何可以解释这种表和 SQL 语句的错误消息的内容。顺便说一句,该表不包含任何将

NULL
作为
name
字段值的记录。

sql mariadb
1个回答
0
投票

您可以使用临时表概念来解决此问题。 请按照以下步骤操作:

mysql> select * from nameinfo;+----+--------+---------+------------+----------+
| id | parent | related | created    | name     |
+----+--------+---------+------------+----------+
|  1 |      1 |       2 | 1234567890 | NAME     |
|  2 |      2 |       2 | 1234567890 | x-NAME-1 |
|  3 |      3 |       3 |  123456789 | x-NAME-1 |
|  4 |      4 |       4 |   12345678 | x-NAME-1 |
+----+--------+---------+------------+----------+
4 rows in set (0.00 sec)

mysql> CREATE TEMPORARY TABLE tmpname AS SELECT id, MID(name, 3, LENGTH(name) - 4)
AS new_name FROM nameinfo WHERE LENGTH(name) > 4    AND LEFT(name, 2) = 'x-'    AND RIGHT(name, 2) = '-1'   AND NOT EXISTS (       SELECT 1       FROM nameinfo AS u
     WHERE u.parent = nameinfo.parent          AND u.related = nameinfo.related
      AND u.name = MID(nameinfo.name, 3, LENGTH(nameinfo.name) - 4) );
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> UPDATE nameinfo AS plu JOIN tmpname AS tmp    ON plu.id = tmp.id SET plu.name = tmp.new_name;
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> DROP TEMPORARY TABLE tmpname;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from name_info;
ERROR 1146 (42S02): Table 'test.name_info' doesn't exist
mysql> select * from nameinfo;
+----+--------+---------+------------+----------+
| id | parent | related | created    | name     |
+----+--------+---------+------------+----------+
|  1 |      1 |       1 | 1234567890 | NAME     |
|  2 |      2 |       2 | 1234567890 | NAME     |
|  3 |      3 |       3 |  123456789 | NAME     |
|  4 |      4 |       4 |   12345678 | NAME     |
+----+--------+---------+------------+----------+
4 rows in set (0.00 sec)

mysql> 
© www.soinside.com 2019 - 2024. All rights reserved.