PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites

11 Dec 2013

Copy values from one table to another in mysql

                       We can easily copy the values within a table. But can we copy the values from one table to another table?. Yes, you can done it in mysql. The mysql query is:

                       Insert into table2 (column_name1, column_name2) 
                               select column_name1, column_name2 from table1

Consider the example:
 Table1 is name of this table.

Mysql at phponwebsites


Table2 is name of this table.


Mysql at phponwebsites

Copy values from one table1 to table2 in mysql:

                         Now you have to add values from 'Table1' to 'Table2'. The following mysql query is used to do it.
                         Insert into Table2 (No,Game_name,Image)
                              select No,Game_Name, Image from Table1

The output of  'Table2' is:

copy values from one table to another in Mysql at phponwebsites

Now you'll get values in 'Table2' which is same as 'Table1'.

Copy values from one table to another except some values in mysql:


                        If you want to some coulmn values, then you will add only required column name only in mysql query. Suppose you need only particular values. Then you use 'where' clause to get your answer. The mysql query as follow as:
                        
                        Insert into Table2 (No,Game_name,Image)
                              select No,Game_Name, Image from Table1  
                                where  Game_Name='sample1' and Game_Name='sample2'

Then the output look like this:


copy specific values from one table to another in Mysql at phponwebsites


Now you will get values only named in sample1 and sample2.

Related Post: