Merging multiple sql table results into one - mysql

I have imported several large csv's and I am looking to create a merged table from several imports.
So lets say I have 2 tables.
table1:
title
ben
rupert
table2:
title
karen
jill
and I want to either populate an empty table or create one on the fly.
//NewTable
title
ben
rupert
karen
jill
I've tried using SQL like this - but I am getting NewTable undefined variable issues
select *
into `NewTable`
from(
select * from `table1`
union all
select * from `table2`
union all
)t

Create NewTable first then:
INSERT INTO NewTable
SELECT * FROM table1
UNION ALL
SELECT * FROM table2
An alternate way in which you wouldn't need to create the table first, off the top of my head would be:
SELECT *
INTO NewTable
FROM table1
And then perform the insert from the second table:
INSERT INTO NewTable
SELECT * FROM table2

The select ... into ... statement in MySQL is for populating variables. What you are looking for is the insert ... select ... statement:
insert into newtable
select * from ...

insert into table1 select * from table2;

Related

MySQL if / else or select if(contiton,'true','false'); - Syntax-problems

When I use
select if (1=1,'true','false');
then everything is ok and I receive 'true' but if I try
select if (1=1,(select * from table1), (select * from table2));`
I receive syntax error in mysql!
With
if condition then <SQL-Expression1> else <SQL-Expression2> end if;
I have the same problem, when the SQL-Expression is complex like
select * from table1!
If I use a complex SQL-Expression like
select * from table1
or
insert into table1 (select field1 from table2 where
field1>(select Max(field) from table1));
then I always receive a syntax error, when such a expression is included in an if/else-Statement!
How can I fit it, that complex sql-Statements can be choosed?
My problem is:
I made 2 tables like
create table1 (x1 int4, x2 int4, x3 int4);
create table2 (x int4);
insert into table1 values(1,2,3);
insert into table1 values(4,0,5);
I wanted to transponse table1 to table2
For example:
The result should in table2 like this
1
2
3
4
5
If I enlarge table 1 with a new line like
insert into table1 values (6,7,8);
then table2 should be changed to
1
2
3
4
5
6
7
8
I tried to make it in this way
select if ((select count(*) from table2)=0,
(insert into table2 (select x1 from table1 where x1>0)),
(insert into table2 (select x1 from table1 where
x1>(select Max(x) from table1))));
The same also with x2 and x3.
but syntax errors occur!
If I use only
insert into table2 (select x1 from table1 where x1>(select Max(x) from table1));
then it works if table1 ist not empty otherwise I had to do
insert into table2 (select x1 from table1 where x1>0);
Subqueries in the SELECT clause may only return single values; i.e. not more than one result, and not more than one column. You can't "trick" a query into returning a varying number of columns.
Also, the if-then form of "if" is reserved for procedural sql; used in stored procedures, triggers, etc...

MYSQL Union a limit applied to second statement will not return records

Have got a little stuck on a query. I've simplified it down to it's essence.
1st statement is the select before the UNION ALL
2nd statement is the select after the UNION ALL.
Objective: to return all results in the 1st statement and then limit the results of the 2nd statement.
What's happening: If I ask for the limit of 1 then it will not show me any records from the 2nd statement. But will show the 2 records of the 1st statement.
Anyone know how to solve this?
CREATE
OR REPLACE VIEW qrytest
AS (
select *
from
(select field1,field2
from tableOne) as tbl1
UNION ALL
select *
from
(select field1, field2
from tableOne limit 1) as tbl2
);
select *
from qrytest
where field2 = 10
Observe your CREATE VIEW statement; I don't think you need the second select statement as shown below since you are just duplicating a record in that table.
select *
from
(select field1, field2
from tableOne limit 1) as tbl2
Your query can be modified to
CREATE OR REPLACE VIEW qrytest
AS (
select field1,field2
from tableOne
);

Create a new table from merging two tables with union

I have two tables with the same columns.
I can merge them with UNION
select * from table1
union
select * from table2;
How do I create a new table with the same contents and columns as that query?
You can use CREATE TABLE ... SELECT statement.
CREATE TABLE new_table
SELECT * FROM table1
UNION
SELECT * FROM table2;
create table new_table as
select col1, col2 from table1
union
select col1, col2 from table2
Make sure you select same set of columns from tables in union.
Or even you can explicitly define create table (we generally use in our project).
CREATE TABLE IF NOT EXISTS auditlog (
user_id varchar(30) NOT NULL default '',
user_ip varchar(255) NOT NULL default '',
........
........
KEY ie1 (user_id) )
union=(auditlog_2,auditlog_3,auditlog_4) engine=merge insert_method=last;

Mysql: How to insert into table using select where clause

2 Works:
INSERT INTO data._top
SELECT * FROM data.ops
WHERE ID = 'foo'
Works:
SELECT ID FROM data.table
How do I combine the above 2 statements using Mysql
INSERT INTO data._top
SELECT * FROM data.ops
WHERE (SELECT ID FROM data.table)
Are you possibly after something like this:
INSERT INTO `data._top`
SELECT * FROM `data.ops`
WHERE `id` IN (SELECT `id` FROM `data.table`);

whats wrong with this mysql insert query?

insert into tblcustomermachine
(
select * from
((select vch_CustomerID from tblcustomer where tblcustomer.vch_CustomerID='Cust00001' )
union all
(select Rate from tblmachine)) as t );
that table contains 18 cols and this resultset also contains 18 rows yet it shows " Column count doesn't match value count at row 1" . why?
It looks like your table tblcustomermachine has more then the 1 column.
Like Simone answered, update your insert to INSERT INTO tblcustomermachine(col_1) SELECT ...
You may skip the column names during INSERT, however the SELECT needs to return the same amount of columns that the table holds.
AFAIK, you have to declare field name:
insert into tblcustomermachine (col_1, col_2, col_3, ... col_18) (
select t.field1, t.field2, t.field3, ... t.field18 from (
(select vch_CustomerID from tblcustomer where tblcustomer.vch_CustomerID='Cust00001')
union all (select Rate from tblmachine))
as t
);