MySQL copy query records to new table field and update current records

StackOverflow https://stackoverflow.com/questions/22161925

  •  19-10-2022
  •  | 
  •  

سؤال

I'm query the result and I would like to copy customer_id to new table field old_customer_id and the customer_id will replace the latest records.

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

After my query get the result like above, I would like to copy the customer_id to old_customer_id which it will

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

لا يوجد حل صحيح

نصائح أخرى

UPDATE YourTable
SET old_customer_id = customer_id, customer_id = 5

DEMO

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

You can try two separate UPDATE queries. First update the old_c_id field then update the c_id.

something like:

UPDATE customerTable
SET old_c_id = c_id;

Then, set the c_id in another UPDATE query.

Not sure what would happen if you tried both in the same query though, probably works:

UPDATE cTable
SET old_c_id = c_id, c_id = ??;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top