Create a new table from merging two tables with union - mysql

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;

Related

return Mysql value/s that don't find on table records

i have a table
users
id collection_id
1 xwkoss
2 cw2333
3 oipopp
And i run query:
SELECT * FROM USERS WHERE collection_id in ('xwkoss','cw2333', 'abcdeef')
that query work fine and return 2 values existing on table, but i need know, which value doesn't exist on table records example: 'abcdeef', based on search parameters
thanks
Create a table on-the-fly and check with NOT EXISTS:
SELECT user_code
FROM
(
SELECT 'xwkoss' AS user_code
UNION ALL
SELECT 'cw2333' AS user_code
UNION ALL
SELECT 'abcdeef' AS user_code
) codes
WHERE NOT EXISTS
(
SELECT NULL
FROM users
WHERE users.user_code = codes.user_code
)
ORDER BY user_code;

Mysql: How to delete all duplicates that violate UNIQUE constraint

I want to add a UNIQUE index to a table, like this:
ALTER TABLE `mytable` ADD UNIQUE `myunique_name`(`first`, `second`, `third`);
Mysql responds with:
Duplicate entry '1-2-3' for key 'myunique_name'
I know for sure that this combination is just one out of thousands that violate the constraint.
In this special case I know for sure that all the rows that contain the same values in the three specified columns also contain the same data in the other relevant fields (the primary index differs of course, but is irrelevant), therefore all the duplicates can be deleted.
Is there a way to do delete all duplicate entries but keep one (doesn't matter which primary key is kept) so that the unique index can be added?
CREATE TEMPORARY TABLE IF NOT EXISTS MyTable engine=memory
select 1 as id, 1 col1,1 col2,1 col3
union all
select 2 as id, 2 col1,2 col2,2 col3
union all
select 3 as id, 3 col1,3 col2,3 col3
union all
select 4 as id, 4 col1,4 col2,4 col3
union all
select 5 as id, 1 col1,1 col2,1 col3
union all
select 6 as id, 2 col1,2 col2,2 col3
CREATE TEMPORARY TABLE IF NOT EXISTS MyDuplicateTableWithCount engine=memory
select col1 , col2 , col3, count(*) Count_1
from MyTable
group by col1 , col2 , col3
having count(*)>1
select a.* from MyTable a
inner join
(select col1 , col2 , col3
from MyDuplicateTableWithCount
) b
on a.col1 =b.col1 and a.col2 =b.col2 and a.col3 =b.col3
order by a.id
After getting the duplicate id's write your delete query specifyinging duplicate id's as
delete from myTable where id in (5,6)
Also use below query using myTable from above
CREATE TEMPORARY TABLE IF NOT EXISTS MyTable2 engine=memory
SELECT MIN(id) as id, Col1, Col2, Col3
FROM MyTable
GROUP BY Col1, Col2, Col3
DELETE a FROM MyTable as a
LEFT JOIN (
SELECT * from MyTable2
) as b ON
b.id = a.id
WHERE
b.id IS NULL

Merging multiple sql table results into one

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;

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`);

How can disjoint columns be selected in a single query?

I have 4 tables each with different columns but they all have one column in common. This is an integer identifier column. So I will have some integer x, and I want all the rows from all 4 tables that have this one id column equal to x.
I've tried something similar to:
SELECT table1.col1, table2.col2 FROM table1 LEFT JOIN table2 ON table1.id=x OR coastlinessports.id=x
And I get back rows which have both the columns from both tables in the same row.
So one result block would have:
table1.col1, table2.col2
But I really want:
table1.col1
tale2.col2
Is there a way I can do this without doing 4 select queries in a row?
If you want sequential rows from different tables, and for each table to return a different number of rows, then you can use UNION. However, UNION requires each SELECT to return the same number of columns, so you will need to fill in the missing columns with a value (or NULL), like this:
DROP TABLE IF EXISTS `table1`;
DROP TABLE IF EXISTS `table2`;
CREATE TABLE `table1` (
`id` int(11) NOT NULL auto_increment,
`col1` VARCHAR(255),
`col2` VARCHAR(255),
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
CREATE TABLE `table2` (
`id` int(11) NOT NULL auto_increment,
`col1` VARCHAR(255),
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
INSERT INTO `table1` VALUES
(1, '1,1', '1,2'),
(2, '2,1', '2,2');
INSERT INTO `table2` VALUES
(1, '1,1'),
(2, '2,1');
SELECT `id`, `col1`, `col2` FROM `table1` WHERE `id` = 1
UNION
SELECT `id`, `col1`, NULL AS `col2` FROM `table2` WHERE `id` = 1;
+----+------+------+
| id | col1 | col2 |
+----+------+------+
| 1 | 1,1 | 1,2 |
| 1 | 1,1 | NULL |
+----+------+------+
If you want to further process the UNION result set, you can wrap it in another SELECT, like this:
SELECT `col1`, `col2` FROM (
SELECT `id`, `col1`, `col2` FROM `table1` WHERE `id` = 1
UNION
SELECT `id`, `col1`, NULL AS `col2` FROM `table2` WHERE `id` = 1
) AS `t1`
ORDER BY col2;
+------+------+
| col1 | col2 |
+------+------+
| 1,1 | NULL |
| 1,1 | 1,2 |
+------+------+
Is that what you are after?
This probably won't answer your question, but there's something weird about the JOIN.
Usually the "ON" condition refers to both tables being joined, similar to this:
... FROM table1 LEFT JOIN table2 ON table1.id = table2.id ...
I guess there can be cases where you wouldn't do that, but I can't think of any.
You should check out this post. It seems like what you are asking for:
http://ask.sqlteam.com/questions/870/pivoting-multiple-rows-into-one-row-with-multiple-columns
If the table is called the same you can use USING
And for the part of the given value, use WHERE
select * from table1 join table2 using(commonColumn) join table3 using(commonColumn) join table4 using(commonColumn) where commonColumn="desiredValue"
Update: on a second read of your question
You want this?
All rows of table1 where commonColumn="desiredValue"
Followed by
All rows of table2 where commonColumn="desiredValue"
Followed by
All rows of table3 where commonColumn="desiredValue"
Followed by
All rows of table4 where commonColumn="desiredValue"
If that's so, you need to use a UNION (and you have to make 4 selects)
IF the number of columns differs, you need to fill the gaps whit aliases
SELECT col1, col2, col3, col4 from table1 where commonColumn="desiredValue"
UNION
SELECT col1, col2, 0 as col3, 0 as col4 from table2 where commonColumn="desiredValue"
...