concat: Adding string at the end of a field dataWe can append a string or data to an existing data of a field by using concat function of MySQL. Here we are not updating or replacing an existing data with new one, we are just adding the string at the end of the field data.
For example we want to add site signature at the end of the comments posted by users. Here each user posts are different so we can't apply any update command so we will use concat to add the site signature to the each post kept inside a record – field in our update command.
Here is the syntax
concat(field_name,"string to add")Now let us see how it is used in a MySQL table query.
update photo_img set dtl=concat(dtl,'site_data to add') where gal_id='22'The above command will add the string site_data to add at the end of each record dtl field for which gal_id is equal to 22
If we will not use Where condition in sql then all the records will be changed with concat query.
Adding / to dir name stored in MySql tableIn our table we stored directory name as 'html_tutorial' for all the records. We want to update all the records by adding forward slash to the data inside dir name.
UPDATE `table_name` SET dir=concat('/',dir)This will add forward slash to the columns dir for all the records ( at left side ).
To add / at end of the data of all records we can use like this.
UPDATE `table_name` SET dir=concat(dir,'/')Try to change some part of the data by using replace command. Try to understand difference between replace and concat.
Number of User Comments : 5