PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: copy data
copy data - phponwebsites.com
Showing posts with label copy data. Show all posts

15 Dec 2013

Copy values from one database to another in mysql

                       All of you know, you can copy data within a table and between tables. But can you copy data between two database in mysql. Yes, you can done it in mysql. The following mysql query is used for do this.
Mysql Query:
                         INSERT INTO database2.table_name (column_name)
                              SELECT table_name.column_name FROM table_name


Copy data from one database to another in mysql:


                       Consider following example. You've two databases named with db1 and db2. Each database have a table. Table1 is presented in db1 and Table2 is present in db2.
The Table1 in db1 is look like this:

Mysql at phponwebsites

The Table2 in db2 look like this:

Mysql at phponwebsites

Now we copy data from database db1 to db2. The mysql query is:
                     
                     INSERT INTO db2.Table2(name,image)
                              SELECT Table1.name, Table2.name FROM Table1

Now you will get output as follow as:

copy values from one database to another in Mysql at phponwebsites

                   Now you'll get values in Table2 as like as Table1 in db.

Copy data from one database to another with specific condition in mysql:


                  You can copy only specific values from one database to another in mysql using 'WHERE' condition in mysql. Consider a example: Now we are going to copy named with 'sample1' and 'sample2' from database db1. The mysql query is:

                     INSERT INTO db2.Table2(name,image)
                              SELECT Table1.name, Table2.name FROM Table1
                              WHERE Game_name!='sample3'

Now you'll get output as follow as:

copy values from one database to another with specific condition in Mysql at phponwebsites

Note:
          You can copy the values from one database to another using  this method only if two databases are in same server.

Related Post:

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:

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: