Copy a table to another table with different structure - mysql

I have 2 tables in the DB:
Table1
Table1_Temp
The Table1_Temp was generated from a CSV. it is almost identical to Table1 but different because all Table1_Temp fields are VARCHAR and it has some irrelevant fields.
I need to move the Data from Table1_Temp to Table1 but to keep the structure of Table1, and disregard the unnecessary fields from Table1_Temp.
How can I do it?

Choose the columns to use and cast them to the necessary type in your select
insert into table1 (col1, col2, col3)
select cast(col1 as signed), col5, col7
from Table1_Temp

if both tables are on a different database (and different column)
INSERT INTO db1.table1 (Acol1, Acol2, Acol3)
SELECT Bcol1 AS Acol1, Bcol2 AS Acol2, Bcol3 AS Acol3
FROM db2.table1_temp
This will only work if both databases are under 1 server (in this instance "localhost")

Related

mysql select records in difference between two tables

I just create a federated table (cron_task_sync) get the updated data from server. So now I want to update the outdated table (cron_task) in my local mysql database with that table.
Any sql can do this? I found there's a lot of limitation when I use mysql. For instance except cannot be used.
I am sure that that two table are in the same structure. Please help me.
If you have a primary key, like id, you could use not in:
insert into cron_task
(id, col1, col2, col3, ...)
select id
, col1
, col2
, col3
from cron_task_async
where id not in
(
select id
from cron_task
)

Select Insert Query by mentioning all columns across databases

I am working on redesigning of a legacy db and I have set new names to columns of old db. So, for instance, if olddb.oldtable under dbold has column descr, I have set it as description in new newdb.netable for column.
How can I mention individual columns in my query?
I am using MYSQL
Update: Both Databases are on different IP Addresses and I am using Navicat to transfer data.
You can try like this:
INSERT INTO newtable (col1, col2, ..., )
SELECT col1, col2, ..., FROM oldtable
By trying the above query you can insert the specific column. So for example if your newtable has a column as description and old table as descr then you can mention it like:
INSERT INTO newtable (col1, col2, `description`, ..., )
SELECT col1, col2, `descr` ,..., FROM oldtable
Also if the table column list is large and you want to copy all the columns and its data then you can simply use the wildcard charater * as:
INSERT INTO newtable
SELECT * FROM oldtable;
You can insert all columns at once without the need to mention the names using this:
INSERT INTO newtable (SELECT * FROM oldtable);
It will make an 1x1 match independently of column names.
If types don't match then will insert default values (not checked for all the type combination).
Note that column number must be the same on both tables otherwise an error like this will occur:
#1136 - Column count doesn't match value count at row 1

How to move row to another row in mysql

I want to move one or two rows(with data) for e.g. from end to first position or from middle to end? With phpmyadmin with GUI there isn't option to moving rows.
Another question:
How to move one table to another table by copying data?
Rows position in a resultset are determined by ORDER BY clause or by "chance" if it's not specified, so moving from a position to another in absolute has no meaning.
You can use INSERT SELECT statement to copy data from table1 to table2 if they have the same structure.
INSERT INTO table2
SELECT *
FROM table1
You may copy data with help of a query:
INSERT INTO table2 (col1, col2, col3) SELECT col1, col2, col3 FROM table1

Inserting data from one table to another without duplication in access-VBA

I want to insert data from a table WorkTableA to another table TableA, without duplicating the data (i.e. do not insert into WorkTableA if the customer name already exists).
Is there a way of doing it through VBA code.
The field name and their properties in both tables are identical.
What you need is an INSERT INTO statement
INSERT INTO WorkTableA
( CustomerName, Col2, Col3...)
SELECT CustomerName, Col2, Col3
FROM TableA LEFT JOIN WaorkTableA ON TableA.CustomerName = WORKTABLEA.CustomerName
WHERE WorkTableA.CustomerName IS NULL
Something like this might work.
The SELECT part of the statement will select only the ones that DO NOT EXIST in WorkTableA

SQL Synchronising large table data

Hi i m using SQL 2008 R2.What is the faster way to insert data of 10 mil. records from one table A into another empty table B.
Table A and B dont have same schema similar but not the same.
It's only 10 million rows. Just use a normal INSERT
INSERT tableB (col1, col2, col3, ...)
SELECT col1, col2, col3, ... FROM tableA
or
--assumes no table b already
SELECT col1, col2, col3, ..., INTO TableB FROM tableA
.. now add some columns etc
Your using sql?
why not create a new table
and then insert records from table a into new table b??