0

I have two tables: p.Test and q.Test on two different Databases - p and q.

Both these tables have different MYSQL connections and are on two different servers.

I need to do a Natural Join as is given here : http://www.microshell.com/database/sql/comparing-data-from-2-database-tables/2/

How do I possibly do that? I am using java PreparedStatement as well to actually run the query. Is there a way to possibly do this?

Say I create two Connections to each of them:

Connection conn1 = DriverManager.getConnection(URLtoPDB, Username,Password);
Connection conn1 = DriverManager.getConnection(URLtoQDB, Username,Password);

How do I fire a preparedStatement to even do say: select * from p.Test,q.Test

gran_profaci
  • 8,087
  • 15
  • 66
  • 99

4 Answers4

0

You can specify the database name before the table name in query as below

SELECT
    *
FROM
    [yourdatabase1].[jajal].[j]
    NATURAL LEFT JOIN [yourdatabase2].[jajal_copy] jc
WHERE
     jc.user_id IS NULL
UNION ALL
SELECT
   *
FROM
    [yourdatabase1].[jajal].[j]
NATURAL RIGHT JOIN [yourdatabase2].[jajal_copy] jc
WHERE
    j.user_id IS NULL

you can user servernames for different servers as

 select
    *
 from
    LocalTable,
      [OtherServerName].[OtherDB].[dbo].[OtherTable]
Mahesh
  • 8,694
  • 2
  • 32
  • 53
0

From comments in other answers, it looks like you need to compare two tables from different database servers. In that case, you have to use federated storage engine. This is a change that has to be done on the database server. You can't do that in your Java code.

If you have to do it in Java and you cannot modify anything in the database, you have to download all the data to memory and compare them manually.

Loša
  • 2,621
  • 2
  • 14
  • 19
  • Any efficient way to do this? How about getting all the data in two Hashmaps and comparing them? http://stackoverflow.com/questions/12721735/how-to-receive-difference-of-maps-in-java – gran_profaci Jul 03 '13 at 09:30
  • 1
    Wouldn't it be better to setup [database replication](http://dev.mysql.com/doc/refman/5.0/en/replication.html) to keep your tables in sync, so you don't have to care about it manually? – Loša Jul 03 '13 at 09:42
0

Do your replica again, into another table of current database and then compare.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-1

If you are using mysql then database is equivalent to schema. so it is straight forward

select * 
from p.Test natural join q.Test;
chetan
  • 2,876
  • 1
  • 14
  • 15