更新MySQL字段; condition =两个数字之间的字符串字段

时间:2012-06-25 15:37:37

标签: mysql mysql-error-1292

如果字符串列在两个数字之间,我正在尝试更新字段的值。

UPDATE SAMPLE.EXAMPLE 
SET modNum = CONCAT(modNum,"26")
WHERE modNum NOT LIKE '%26%' 
  AND (procKey BETWEEN 90000 AND 99123 OR procKey = 77444);

不幸的是我收到了错误:

  

错误代码:1292。截断错误的DOUBLE值:'4123F'

我感到很惊讶,因为当我选择时,我会得到预期的结果。

SELECT *
FROM SAMPLE.EXAMPLE 
WHERE modNum NOT LIKE '%26%' 
  AND (procKey BETWEEN 90000 AND 99123 OR procKey = 77444);

创建表语句:

'CREATE TABLE `example` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `modNum` char(4) DEFAULT NULL,
  `procKey` char(8) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1'

示例数据:

INSERT INTO `SAMPLE`.`EXAMPLE` (`modNum`, `procKey`) VALUES ('42', '99001');
INSERT INTO `SAMPLE`.`EXAMPLE` (`modNum`, `procKey`) VALUES ('42', '9900f');

根据我的条件,我该怎么做才能更新列?



对我而言,它看起来像一个bug。我能想到的最好的方法是使用正则表达式检查procKey的值是否为整数。我将UPDATE语句更改为:

UPDATE SAMPLE.EXAMPLE 
SET modNum = CONCAT(modNum,"26") 
WHERE modNum NOT LIKE '%26%' 
    AND procKey REGEXP '^[0-9]+$' 
    AND (procKey BETWEEN 90000 AND 99123 
        OR procKey = 77444);

如果有更好的方法,请告诉我。

3 个答案:

答案 0 :(得分:1)

对我而言,它看起来像一个bug。我能想到的最好的方法是使用正则表达式检查procKey的值是否为整数。我将UPDATE语句更改为:

UPDATE SAMPLE.EXAMPLE 
SET modNum = CONCAT(modNum,"26") 
WHERE modNum NOT LIKE '%26%' 
    AND procKey REGEXP '^[0-9]+$' 
    AND (procKey BETWEEN 90000 AND 99123 
        OR procKey = 77444);

如果有更好的方法,请告诉我。

答案 1 :(得分:0)

试试这个:

UPDATE SAMPLE.EXAMPLE
SET modNum = CONCAT(modNUM,"26") 
WHERE modNum NOT LIKE '%26%' 
  AND (procKey BETWEEN 90000 AND 99123 OR procKey = 77444);

答案 2 :(得分:0)

它适用于我,所以我',非常肯定它必须是一些错误。

mysql> CREATE TABLE `example` (
    ->   `id` int(11) NOT NULL AUTO_INCREMENT,
    ->   `modNum` char(4) DEFAULT NULL,
    ->   `procKey` char(8) DEFAULT NULL,
    ->   PRIMARY KEY (`id`)
    -> ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1
    -> ;
Query OK, 0 rows affected (0.04 sec)

mysql> UPDATE EXAMPLE                                                               
-> SET modNum = CONCAT(modNum,"26")
    -> WHERE modNum NOT LIKE '%26%' 
    ->   AND (procKey BETWEEN 90000 AND 99123 OR procKey = 77444);
ERROR 1146 (42S02): Table 'SOF.EXAMPLE' doesn't exist
mysql> UPDATE example 
    -> SET modNum = CONCAT(modNum,"26")
    -> WHERE modNum NOT LIKE '%26%' 
    ->   AND (procKey BETWEEN 90000 AND 99123 OR procKey = 77444);
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.5.9     |
+-----------+
1 row in set (0.00 sec)

编辑:

mysql> INSERT INTO `example` (`modNum`, `procKey`) VALUES ('42', '99001');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO `example` (`modNum`, `procKey`) VALUES ('42', '9900f');
Query OK, 1 row affected (0.00 sec)
mysql> UPDATE example  SET modNum = CONCAT(modNum,"26") WHERE modNum NOT LIKE '%26%'    AND (procKey BETWEEN 90000 AND 99123 OR procKey = 77444);
Query OK, 0 rows affected, 2 warnings (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 2

mysql> show warnings;
+---------+------+----------------------------------------------+
| Level   | Code | Message                                      |
+---------+------+----------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: '9900f   ' |
| Warning | 1292 | Truncated incorrect DOUBLE value: '9900f   ' |
+---------+------+----------------------------------------------+
2 rows in set (0.00 sec)