0

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        |
+-------------+-----------------+

3 Answers 3

0
UPDATE YourTable
SET old_customer_id = customer_id, customer_id = 5

DEMO

0

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

0

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 = ??;
2
  • How about the c_id, I would like to grab the latest c_id for replace the rest in table customer_id field
    – Jien Wai
    Mar 4, 2014 at 3:19
  • if you want latest, in terms of time, you'd need some sort of date field first. If you mean latest as in the largest number, you could use a sub query in the update: ... c_id = (SELECT MAX(c_id) FROM cTable); Mar 4, 2014 at 3:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.