PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: Copy values from one column to another within table in mysql

13 Dec 2013

Copy values from one column to another within table in mysql

                      Mostly we had copied only particular values from one table to another. Similarly we had copied only particular values within a table. Now the question is rise. That is, can copy the whole column values to another column in same table? Is it possible in mysql?. Yes it is possible in mysql. We can copy the all values in one column to another within a table.
                     Consider a following example:
                                   Table1 is a table name.
                                               
Mysql at phponwebsites
                   
 Now you need to add another column with named as 'Author'. The mysql query is:
                     Alter table Table add column Author varchar(30)

add column in Mysql at phponwebsites

                     You need to copy the Game_Name column values to Author column. The mysql query as follows as:
                              
                               Update tablename set column2=column1

That means, Update Table1 set Author=Game_Name.

Now You will get following as a output:

copy values from one column to another in same table Mysql at phponwebsites

          
                             Now you will get the output both the Game_name and Author column values are same.


Copy values from one column to another except some values within table:


                           Suppose you have to copy all values in a column except some value to another column within a table. It can be done by following mysql query:

                           Update Table1 set Game_Name=Author where Game_Name!='sample1'

Your output look like this:

copy only selected values from one column to another in Mysql at phponwebsites

Now you'll get all values from one column to another except some value.

Related Post:

2 comments: