我會做如下
- 首先把備份的表。
- 然後添加一個帶有十進制的新列(20,3)
- 然後通過做一些字符串操作來更新舊列的值。
- 刪除舊列
- 重命名新列。
下面是測試情況
mysql> create table test (no decimal(20,2));
Query OK, 0 rows affected (0.20 sec)
mysql> insert into test values('10.01'),('10.09'),('10.10'),('10.11'),('10.19'),('10.99');
Query OK, 6 rows affected (0.03 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from test;
+-------+
| no |
+-------+
| 10.01 |
| 10.09 |
| 10.10 |
| 10.11 |
| 10.19 |
| 10.99 |
+-------+
6 rows in set (0.00 sec)
mysql> alter table test add column no_new decimal(20,3);
Query OK, 0 rows affected (0.38 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from test;
+-------+--------+
| no | no_new |
+-------+--------+
| 10.01 | NULL |
| 10.09 | NULL |
| 10.10 | NULL |
| 10.11 | NULL |
| 10.19 | NULL |
| 10.99 | NULL |
+-------+--------+
6 rows in set (0.00 sec)
然後
mysql> update test set no_new = concat(substring_index(no,'.',1),'.',concat('0',substring_index(no,'.',-1)));
Query OK, 6 rows affected (0.05 sec)
Rows matched: 6 Changed: 6 Warnings: 0
mysql> select * from test;
+-------+--------+
| no | no_new |
+-------+--------+
| 10.01 | 10.001 |
| 10.09 | 10.009 |
| 10.10 | 10.010 |
| 10.11 | 10.011 |
| 10.19 | 10.019 |
| 10.99 | 10.099 |
+-------+--------+
6 rows in set (0.00 sec)
mysql> alter table test drop column no;
Query OK, 0 rows affected (0.36 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table test change no_new no decimal(20,3);
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from test ;
+--------+
| no |
+--------+
| 10.001 |
| 10.009 |
| 10.010 |
| 10.011 |
| 10.019 |
| 10.099 |
+--------+
6 rows in set (0.00 sec)
看起來並不像一個真正的需求 – GurV
嗯,轉換成varchar和比str_replace函數(」。 「」 .0" ,否)......並轉換爲十進制(20,3 )..也許你是對的:D –
不適用於'10.100' – GurV