Update column including its own column info

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Update column including its own column info

Post by kryles »

Hi,

I remember reading this somewhere but couldn't find it again so I was hoping someone could point me in the right direction.

I want to do something simple like

Code: Select all

 
update tablename
set column1 = column1 + 'this string'
where columnkey = 106
 
instead of doing a select for the column, adding the string and then updating the new information. My hosting service uses MySQL 4.0.24 if that makes any difference in SQL version.

column1 is empty by default but is a varchar type

Regards,
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Update column including its own column info

Post by Benjamin »

Code: Select all

 
UPDATE tablename
  SET column1 = CONCAT(column1, 'this string')
WHERE
columnkey = 106
 
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: Update column including its own column info

Post by kryles »

Thanks astions :D

My example seemed to not be working when the field was empty, but when it wasn't the + worked fine. Just thought I'd mention that in case someone comes across this thread for future reference
Post Reply