MySQL将查询记录复制到新表字段并更新当前记录

时间:2014-03-04 02:50:48

标签: mysql

我正在查询结果,我想将customer_id复制到新表格字段old_customer_idcustomer_id将替换最新记录。

+-------------+-----------------+
| customer_id | old_customer_id |
|-------------+-----------------+
|      5      |                 |
|      3      |                 |
|      1      |                 |
+-------------+-----------------+

在我的查询获得上述结果后,我想将customer_id复制到old_customer_id它将

+-------------+-----------------+
| customer_id | old_customer_id |
|-------------+-----------------+
|      5      |        5        |
|      5      |        3        |
|      5      |        1        |
+-------------+-----------------+

3 个答案:

答案 0 :(得分:0)

UPDATE YourTable
SET old_customer_id = customer_id, customer_id = 5

DEMO

答案 1 :(得分:0)

您可以尝试两个单独的UPDATE个查询。首先更新old_c_id字段,然后更新c_id

类似的东西:

UPDATE customerTable
SET old_c_id = c_id;

然后,在另一个c_id查询中设置UPDATE

不确定如果您在同一个查询中尝试过这两种情况会发生什么,可能有效:

UPDATE cTable
SET old_c_id = c_id, c_id = ??;

答案 2 :(得分:0)

UPDATE table_name SET old_customer_id = customer_id,customer_id = @your_new_customer_id;