开发者

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.

开发者_如何学C

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜