Question

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

Was it helpful?

Solution

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]

OTHER TIPS

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.

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

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

select * 
from p.Test natural join q.Test;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top