0

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

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');

给定我的条件,我可以做些什么来更新列?



对我来说,它看起来像一个错误。我能想到的最好方法是使用正则表达式检查 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);

请让我知道是否有更好的方法。

4

3 回答 3

1

对我来说,它看起来像一个错误。我能想到的最好方法是使用正则表达式检查 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);

请让我知道是否有更好的方法。

于 2012-06-26T12:29:17.093 回答
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)
于 2012-06-25T16:35:39.330 回答
0

尝试这个:

UPDATE SAMPLE.EXAMPLE
SET modNum = CONCAT(modNUM,"26") 
WHERE modNum NOT LIKE '%26%' 
  AND (procKey BETWEEN 90000 AND 99123 OR procKey = 77444);
于 2012-06-25T15:44:02.583 回答