我应该在字符串前加一个零
首先,我选择我的数据进行验证
mysql> SELECT cp, LENGTH(cp), CONCAT(0, `cp`) FROM communes WHERE LENGTH(cp) < 5
ORDER BY cp ASC LIMIT 4;
+------+------------+-----------------+
| cp | LENGTH(cp) | CONCAT(0, `cp`) |
+------+------------+-----------------+
| 1000 | 4 | 01000 |
| 1000 | 4 | 01000 |
| 1000 | 4 | 01000 |
| 1090 | 4 | 01090 |
+------+------------+-----------------+
4 rows in set (0.03 sec)
它运作良好。
第二,我尝试更新
mysql> UPDATE communes SET cp = CONCAT(0,`cp`) WHERE LENGTH(cp) < 5;
Query OK, 0 rows affected (0.07 sec)
Rows matched: 3447 Changed: 0 Warnings: 0
我不明白为什么它不起作用
信息
mysql> describe communes;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | | NULL | |
| cp | int(6) | NO | | NULL | |
| dept | varchar(255) | NO | | NULL | |
| code_insee | int(11) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
十分感谢