I want to split a table into two tables (or more, but let's say two).
table_original
id column1 column2
1 1 2
2 1 3
3 1 4
4 1 4
5 1 5
We can also assume that id
is a unique identifier. Now I split this table into two, by using a CREATE TABLE table1 AS SELECT * FROM table_original WHERE column2 <= 4
and CREATE TABLE table2 AS SELECT * FROM table_original WHERE column2 >= 4
. Now I have these two tables:
table1
id column1 column2
1 1 2
2 1 3
3 1 4
4 1 4
table2
id column1 column2
3 1 4
4 1 4
5 1 5
How to get the same results from those two tables that I can get from the original table? If I run a query SELECT * FROM table1 UNION SELECT * FROM table2
it will be the same as SELECT * FROM table_original
because of the unique id
value, however if I run a query SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2
it returns:
column1, column2
1 2
1 3
1 4
1 5
which is not the same as SELECT column1, column2 FROM table_original
, which returns:
column1, column2
1 2
1 3
1 4
1 4
1 5
Duplicates from the same table are removed. However, if I wanted to let's say do a count on duplicates, the results will be different, which is bad. So is there a way to do a UNION
type operation but keep duplicates that are found in the same table?