How do copy a modify field of a table to a new field in the same table?
I have a MySQL database table C
with a field called Phone
. I've already created a new field in the C
table called PhoneFixed
and I want to update that field with a modified version of the Phone
field.
The PhoneFixed
field will use the following statement to get its value:
Concat('(',Left(C.Phone,3),') ',Right(C.Phone,8)) As `PhoneFixed`
What statement do I need to automatically update the value of PhoneFixed
to be equal to the result of the statement above for all rows in my table?
If you just want to update a pre-existing PhoneFixed
column for all rows, you'd run a query like this:
UPDATE C
SET PhoneFixed = CONCAT('(', LEFT(Phone, 3), ') ', RIGHT(Phone, 8))
If I understood you correctly, a simple UPDATE will work:
UPDATE C SET PhoneFixed = CONCAT('(', LEFT(Phone, 3), ') ', RIGHT(Phone, 8))
That will update the PhoneFixed using the Phone value from the same row.
精彩评论