0

我正在使用 mysql,需要复制表中的所有行,但我不复制 id 列,并且新生成的编号需要 ex。

我的桌子

              item_db             
+------------------------------------------+    
|  id  |  name      |  price  | detail  | 
+------------------------------------------+
|   1  |  example   |    230  |         3  | 
|   2  |  power     |    110  |         3  | 
|   3  |  voltage   |    1.2  |         4  | 
|   4  |  example   |    240  |         4  | 
|   5  |  example   |    320  |         6  | 
|   6  |  power     |    100  |         4  | 
|   7  |  power     |    110  |         6  | 
|   8  |  example   |    230  |         3  | 
|   9  |  power     |    110  |         3  | 
|   10 |  voltage   |    1.2  |         4  | 
|   20  |  example  |    240  |         4  | 
|   21  |  example  |    320  |         6  | 
|   22  |  power    |    100  |         4  | 
|   23  |  power    |    110  |         6  | 
|   24  |  example  |    240  |         4  | 
|   25  |  example  |    320  |         6  | 
|   26  |  power    |    100  |         4  | 
|   27  |  power    |    110  |         6  | 
|   28  |  example  |    240  |         4  | 
|   29  |  example  |    320  |         6  | 
|   30  |  power    |    100  |         4  | 
+------------------------------------------+

我需要复制

              item_db             
+------------------------------------------+    
|  id  |  name      |  price  | detail  | 
+------------------------------------------+
|   1  |  example   |    230  |         3  | 
|   2  |  power     |    110  |         3  | 
|   3  |  voltage   |    1.2  |         4  | 
|   4  |  example   |    240  |         4  | I need to dup id 1-10 to 101-110
|   5  |  example   |    320  |         6  | 
|   6  |  power     |    100  |         4  | 
|   7  |  power     |    110  |         6  | 
|   8  |  example   |    230  |         3  | 
|   9  |  power     |    110  |         3  | 
|   10 |  voltage   |    1.2  |         4  | 
|   101|  example   |    230  |         3  | 
|   102|  power     |    110  |         3  | 
|   103|  voltage   |    1.2  |         4  | 
|   104|  example   |    240  |         4  | 
|   105|  example   |    320  |         6  | 
|   106|  power     |    100  |         4  | 
|   107|  power     |    110  |         6  | 
|   108|  example   |    230  |         3  | 
|   109|  power     |    110  |         3  | 
|   110|  voltage   |    1.2  |         4  | 
|   20 |  example  |    240  |         4  | 
|   21 |  example  |    320  |         6  | 
|   22 |  power    |    100  |         4  | 
|   23 |  power    |    110  |         6  | 
|   24 |  example  |    240  |         4  | 
|   25 |  example  |    320  |         6  | 
|   26 |  power    |    100  |         4  | i need to dup 20-30 to 200-210
|   27 |  power    |    110  |         6  | 
|   28 |  example  |    240  |         4  | 
|   29 |  example  |    320  |         6  | 
|   30 |  power    |    100  |         4  |
|   200|  example  |    240  |         4  | 
|   201|  example  |    320  |         6  | 
|   202|  power    |    100  |         4  | 
|   203|  power    |    110  |         6  | 
|   204|  example  |    240  |         4  | 
|   205|  example  |    320  |         6  | 
|   206|  power    |    100  |         4  | 
|   207|  power    |    110  |         6  | 
|   208|  example  |    240  |         4  | 
|   209|  example  |    320  |         6  | 
|   210|  power    |    100  |         4  |  
+------------------------------------------+

谢谢专家。

4

1 回答 1

1

您可以使用INSERT ... SELECT

INSERT INTO item_db (id, name, price, detail)
SELECT id+180, name, price, detail FROM item_db WHERE id BETWEEN 20 AND 30

您究竟想如何治疗id并不完全清楚。例如,如果您希望自动分配值,那么您可以(像往常一样)从语句中AUTO_INCREMENT省略它。INSERT

于 2013-01-18T23:17:18.990 回答