PHP MySQL - Join tables with same content?

Ok so I know we can join tables 'across' in MySQL to tables using a id column.

I have two databases where I need to join two tables that are almost identical.

I need to join these 'down', e.g similar to inserting the whole of one table into the other. Not a normal cross join.

Does MySQL allow me to do this?

Upvotes: 0

Views: 94

Answers (4)

maaz
maaz

Reputation: 4493

Yes you can do that, just use full database name before table, make sure your user have permissions on both databases.

 SELECT ...
 FROM database1.table t1
 JOIN database2.table2 t2 ON t2.column = t1.col

Upvotes: 0

Sashi Kant
Sashi Kant

Reputation: 13465

Try this :

INSERT INTO database1.table1 
    SELECT * FROM database2.table2

Upvotes: 0

Mike
Mike

Reputation: 781

UNION is a good way to accomplish this.

SELECT * FROM table1
UNION
SELECT * FROM table2

Just make sure number of columns being returned are the same, and data type matches.

Upvotes: 1

shadyyx
shadyyx

Reputation: 16055

Yes, You can use UNION - the only condition is to select the same amount of columns from both select quesries (both tables):

SELECT a, b, c, d FROM table1

UNION

SELECT d, e, f, g FROM table2

Common issue is to order the results from UNION, so here is solution:

SELECT a AS order_by, b, c, d FROM table1
UNION
SELECT d, e, f AS order_by, g FROM table2
ORDER BY order_by DESC

Upvotes: 1

Related Questions