2016-12-26 53 views
2

後,我有下一列,十進制(20,2)在MySQL表:更新十進制列,讓更多的數字小數點

no 
10.01 
10.09 
10.10 
10.11 
10.19 
10.99 

什麼是更新十進制值最簡單的方法:

no 

10.001 
10.009 
10.010 
10.011 
10.099 
.. 
10.100 
10.101 

如果我換柱成十進制(20,3),我得到下一個數字: 10.010,10.090 ... 10.990等等。每個號碼必須是唯一的。如果MySQL無法做到這一點,如何使用PHP做到這一點?

+0

看起來並不像一個真正的需求 – GurV

+0

嗯,轉換成varchar和比str_replace函數(」。 「」 .0" ,否)......並轉換爲十進制(20,3 )..也許你是對的:D –

+0

不適用於'10.100' – GurV

回答

0

我會做如下

  • 首先把備份的表。
  • 然後添加一個帶有十進制的新列(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) 
+0

它的工作原理,沒有刪除唯一索引。謝謝。 –

0
*****Using MySql***** 

--> SELECT FORMAT('COLUMN NAME',DECIMAL VALUE); 

    *****FOR EXAMPLE:***** 

--> SELECT FORMAT(NO,3); 


     *****QUERY***** 

     Create table '#test1' 
     (no decimal(5,3)); 

insert into '#test1' values 
('10.01'), 
('10.09'), 
('10.10'), 
('10.11'), 
('10.19'), 
('10.99'); 

     ****RESULT**** 
      [![DEMO IMAGE][1]][1] 

    [1]: https://i.stack.imgur.com/AzZpB.jpg 
2

你可以使用這樣的查詢。首先將您的字段設置爲十進制(20,3): 分兩步執行:首先也添加1以防止重複條目。第二扣1

第一步

UPDATE youtTable 
set val = 
    CAST(val as unsigned integer) +1 
    + (val - CAST(val as unsigned integer))/10; 

第二步

UPDATE youtTable 
set val = val -1; 

,但你也可以做一個查詢,如果你先改變最小的數字:

UPDATE yourTable 
set val = 
    CAST(val as unsigned integer) 
    + (val - CAST(val as unsigned integer))/10 
ORDER by val ASC; 

樣品

mysql> SELECT CAST(10.19 as unsigned integer) + (10.19 - CAST(10.19 as unsigned integer))/10; 
+----------------------------------------------------------------------------------+ 
| CAST(10.19 as unsigned integer) + (10.19 - CAST(10.19 as unsigned integer))/10 | 
+----------------------------------------------------------------------------------+ 
|                  10.019000 | 
+----------------------------------------------------------------------------------+ 
1 row in set (0,00 sec) 

mysql> 
+0

每個號碼必須是唯一的( –

+0

@Vadim Nosyrev - 是的,問題是什麼,每個號碼在更新之前和更新後是唯一的,還是你想在該字段中設置新號碼? –

+0

Forexample 509.100和509.010。 querry 509.100必須更改爲509.010,並且我有錯誤重複條目'509.010'。它工作正常,如果刪除唯一索引 –