MYSQL: Merge two tables into one, with union - mysql

I have to make a table out of two other tables (and use union). The query that works is:
SELECT * FROM tabel1
UNION
SELECT * FROM tabel2
Now what i have to do is put this result(data) into table3 (a table i already have, with the same columns as in table1 and table2).
Who can help me?

INSERT INTO table3
SELECT * FROM tabel1
UNION
SELECT * FROM tabel2
since you have the same columns in all three of them ...
In a general case you should work with column lists like
INSERT INTO table3 (col1, col2, col3)
SELECT col1, col2, col3 FROM tabel1
UNION
SELECT col1, col2, col3 FROM tabel2
This way you avoid trouble with auto_increment id-columns. Also You should consider using UNION ALL since UNION filters out duplicate lines and therefore will take longer on large tables.

Related

Join 2 tables with all same column headers but one different

How do I join 2 tables that have all of the same column headers but one?
The other 3 columns are all the same then one table has the column header "January" and the other table has the column header "February"
So I would like to end up with 5 total columns in new table
I would also like all of the data in there even if there are duplicates. Before I used a union all but now I cannot do that because not all my headers are the same.
Thanks!
You can use UNION and rename one of your columns:
SELECT id, name, age, xxx FROM table1
UNION
SELECT id, name, age, yyy AS xxx FROM table2
If you add more information, I can elaborate my answer
select tb1.col1, tb1.col2, tb1.col3, tb2.col3 from tb1
inner join tb2 on (tb1.col1 = tb2.col1)
Assuming uniqueness when just considering the three columns, joining them
SELECT tab1.col1,
tab1.col2,
tab1.col3,
tab1.january,
tab2.february
FROM tab1,
tab2
WHERE tab1.col1 = tab2.col1
AND tab1.col2 = tab2.col2
AND tab1.col3 = tab2.col3
This assumes all rows in tblA have corresponding ones in tblB and vice versa:
SELECT * FROM tblA INNER JOIN tblB USING (col1, col2, col3);
If that is not the case, and depending on the data (if tbl1.january can have legitimate null values, this won't work), you could try something like (didn't check for sure, might need massaged with parenthesis, subselecting, etc...):
SELECT *
FROM tbl1
LEFT JOIN tbl2 USING (col1, col2, col3)
UNION ALL
SELECT * FROM tbl2 LEFT JOIN tbl1 USING (col1, col2, col3)
HAVING tbl1.`january` IS NULL
;

select only one of the identical rows (specific) between two tables in a union

i have have two identical tables
lets call them t1 and t2
and i want to show data from both of them BUT they may have identical rows (lets say these rows have the same id )
in that case i only want to get the row from table 1 and ignore the one from second table .
so
SELECT column_name(s) FROM t1 UNION SELECT column_name(s) FROM t2
but how should i handle duplicates ?
you mean this ?
select column1 , column2 from (
SELECT column1 , column2 FROM t1
UNION
SELECT column1 , column2 FROM t2
)t
group by column1
you will have distinct column1 here
If you want to remove duplicates from a SELECT result set, you could use the DISTINCT clause with a sub-query
SELECT DISTINCT * FROM (SELECT value FROM t1 UNION SELECT value FROM t2) AS S
Or better, you might use the UNION DISTINCT syntax:
SELECT value FROM t1 UNION DISTINCT SELECT value FROM t2;
BTW, for UNION the default is UNION DISTINCT (whereas for SELECT, SELECT ALL is the default), so this could be rewritten as:
-- without specifier UNION is implicitly DISTINCT
SELECT value FROM t1 UNION SELECT value FROM t2;
... which is in fact the query you proposed. What was wrong with that? It works with my test set: http://sqlfiddle.com/#!2/d4812/1
Maybe a sqlfeedle with your actual table content might help to provide a better answer.
Here is one way:
SELECT column_name(s)
FROM t1
UNION
SELECT column_name(s)
FROM t2
where not exists (select 1
from t1
where t1.col1 = t2.col1 and t1.col2 = t2.col2 and . . .
)

Needs help in database query

I have two tables Table1 and Table2. There are 10 fields in Table1 and 9 fields in Table2. There is one common column in both the tables i.e. AdateTime. This column saves unix time stamp of user actions. I want to display records from both the tables as a single result but sorting must me according to AdateTime. Recent action should be display first. Sometimes many recent actions in Table1 but few in Table2. Vice versa is also possible. So I want to fetch combine result set from both the tables using single query. I am using PHP MySQL.
Try
SELECT t1.*, t2.*
FROM table1 t1 INNER JOIN table2 t2
ON t1.AdateTime = t2.AdateTime
ORDER BY t1.AdateTime
or (if tables are not related)
SELECT * FROM
(SELECT ADateTime, col1, col2, col3, col4 FROM table1
UNION
SELECT ADateTime, col1, col2, 1 AS col3, NULL AS col4 FROM table2) t2
ORDER by ADateTime
I would use UNION ALL with an inline view. So something like
select col1,col2,col3,col4,col5,col6,col7,col8,col9,AdateTime
from
(
select col1,col2,col3,col4,col5,col6,col7,col8,col9,AdateTime from Table1
UNION ALL
select col1,col2,col3,col4,col5,col6,col7,col8,null as col9,AdateTime from Table2
) t
order by t.Adatetime desc;
yes you can do it. you just need to join these 2 tables with a join condition. when the join condition matches for a row only that row ll be displayed then further you can write the Code for any operation. use order by AdateTime
select t1.column_1234,t2.column_1234
from t1 table1 , t2 table2
where t1.matching_column = t2.matching_column
order by t1.AdateTime;
t1.matching_column And t2.matching_column are the Primary And Foreign keys for these tables (Matching Column)

number of rows in a stored procedure

is it possible to do conditional select based on a number of rows in a stored procedure?
E.g. if select * from table1 has no records, then do select * from table2?
Try this:
SELECT col1, col2, ..., coln FROM table1
UNION ALL
SELECT col1, col2, ..., coln FROM table2
WHERE NOT EXISTS (SELECT * FROM table1)
This of course assumes that both tables have exactly the same structure.

Multiple select into?

In MySQL, you can write something like
INSERT INTO t1 (col1) SELECT col1 FROM t2
To copy some data over. What if I want to copy some data over from multiple tables? Can I write something like
INSERT INTO t1 (col1) SELECT col1 FROM t2, SELECT col1 FROM t3
?
i think it should be
INSERT INTO t1 (col1) SELECT col1 FROM t2 UNION SELECT col1 FROM t3
EDIT: Now before you go copying data, you may want to verify using
UNION vs UNION ALL
UNION will remove duplicates in the data.
UNION ALL will produce a simple concatenation of the two result sets.