mysql concatenate multiple existing tables into one new table - mysql

I've done some research but all of the examples that I've found seem too complicated given what I would like to do. I have multiple tables of archived data by year (e.g. archive_2013, archive_2012, etc.) and would like to create a new master table (archive_master) consisting of all of the data from all of the tables. The tables have no keys and only 2 columns, one varchar(120) and the other char(20). I'm hoping that this is as simple and straightforward as I think it is.

A simple UNION will do the trick:
SELECT col1, col2
FROM archive_2013
UNION ALL
SELECT col1, col2
FROM archive_2012
Combine it with an INSERT and you are done:
INSERT INTO full_archive
SELECT col1, col2
FROM archive_2013
UNION ALL
SELECT col1, col2
FROM archive_2012

So you want to create one new table?
You could use a simple INSERT INTO with a SELECT
See:
CREATE TABLE with SELECT
http://dev.mysql.com/doc/refman/5.0/en/create-table-select.html
INSERT INTO TABLE with SELECT
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
Example
You can create a new table:
create table 'xyz' select * from archive_2010;
insert into xyz select * from archive_2011;

INSERT INTO archive_master VALUES (SELECT * FROM archive_2013);

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

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

Create new table with calculated data from another table

I'm going to do an regression analysis through my data chunks. For that I need to find out various values. For each data set I need to get N:count(X) sumX sumY sumX*X etc.
Separately I wrote queries for those operations like
SELECT COUNT(X) FROM table_name
SELECT SUM(X*X) FROM table_name
I need to create another table which a row contain count(X), sumX , sumX*X etc. How can I write that kind of query?
You can add multiple aggregates to the same query and use create table as:
create table yournewtable as
select count(x) cnt, sum(x*x) sumxx, sum(x) sumx
from table_name
SQL Fiddle Demo
This will return you a single row. If you need to break it apart, look into group by.
CREATE TABLE first and then use INSERT INTO
CREATE TABLE yourTableName
(
col1 int,
col2 int,
col3 int
);
INSERT INTO yourTableName (col1, col2, col3)
SELECT
(SELECT COUNT(X) FROM table_name),
(SELECT SUM(X) from table_name),
(SELECT SUM(X*X) from table_name)

Mysql error: 1222 when I am creating a new table from two existing tables

Mysql error: 1222 when I am creating a new table from two existing tables.
This is my sql statement for creating a new table from two existing table. Please help me to find solution for this problem, Thanks
CREATE TABLE insurance_db.new_insurance
SELECT * FROM insurance_db.auto_insurance
UNION all
SELECT * FROM insurance_db.home_insurance;
Try this way
CREATE TABLE insurance_db.new_insurance
As (SELECT col1, col2,.... FROM insurance_db.auto_insurance, insurance_db.home_insurance);
It seems that you trying to unite SELECTs with different count of columns.
For example:
SELECT 1 -- one column
UNION ALL
SELECT 1, 2 -- two colums
Causes this error:
Error Code: 1222. The used SELECT statements have a different number of columns
Try to use determined columnt-list for both selects:
CREATE TABLE insurance_db.new_insurance
SELECT
col1, col2, col3 ...
FROM
insurance_db.auto_insurance
UNION ALL
SELECT
col1, col2, col3 ...
FROM
insurance_db.home_insurance;