Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join tables from two different server

Tags:

mysql

I have two different server server1 and server2, now I have db1 in server1 and db2 in server2. I am trying to join these two table in MySQL like this.

Select a.field1,b.field2   FROM  [server1, 3306].[db1].table1 a   Inner Join [server2, 3312].[db2].table2 b   ON a.field1=b.field2   

But I am getting error. Is is possible in MYSQL.

like image 418
Gulrej Avatar asked Jun 20 '12 06:06

Gulrej


People also ask

How can I join two tables in another server in SQL Server?

There are 2 steps to join tables from different servers. The first step is to link the SQL Servers. The next and the last step is to join the tables using the select query having the server name as prefix for the table name.

Can you join 2 tables from different databases?

SQL Server allows you to join tables from different databases as long as those databases are on the same server. The join syntax is the same; the only difference is that you must fully qualify table names.


2 Answers

Yes, it is possible in MySQL.

There are similar questions asked previously too. You have to use FEDERATED ENGINE to do this. The idea goes like this:

You have to have a federated table based on the table at another remote location to use the way you want. The structure of the table have to exactly same.

CREATE TABLE federated_table (     id     INT(20) NOT NULL AUTO_INCREMENT,     name   VARCHAR(32) NOT NULL DEFAULT '',     other  INT(20) NOT NULL DEFAULT '0',     PRIMARY KEY  (id),     INDEX name (name),     INDEX other_key (other) ) ENGINE=FEDERATED DEFAULT CHARSET=latin1 CONNECTION='mysql://fed_user@remote_host:9306/federated/test_table'; 

[Source Answer]

like image 140
Starx Avatar answered Oct 05 '22 00:10

Starx


Replication will be alternate and suitable solution.

server1 - db1 -> replicate to server2. (now db1 and db2 will be in same server server2. join will be easy).

NOTE: If the server2 is enough capable of take the load of db1 in terms of store/process etc., then wen can do the replication. As @brilliand mentioned yes Federated will make the much manual work and slow in process.

like image 30
naveen_sfx Avatar answered Oct 05 '22 02:10

naveen_sfx