0

How to retrieve data from two different MySQL database in a single query using jdbc?

SELECT 
      a.`driver_id`,
      a.`state`, 
      a.`lat`, 
      a.`long`, 
      MAX(a.`time_stamp`) AS timestamp,
      b.vehicle_type 
 FROM `rl_driv_location` AS a, `rl_driver_info` AS b 
 WHERE a.`state` = 1 AND a.`driver_id` = b.`user_id` 
       AND b.`vehicle_type` = '"+ cartype +"'  
 GROUP BY driver_id
  • location database having the rl_driv_location table.
  • core database having the rl_driver_info table.

1 Answer 1

1

Just specify the name of the databases where you want to retrieve data. See below.

SELECT 
      a.`driver_id`,
      a.`state`, 
      a.`lat`, 
      a.`long`, 
      MAX(a.`time_stamp`) AS timestamp,
      b.vehicle_type 
 FROM location.`rl_driv_location` AS a, core.`rl_driver_info` AS b 
 WHERE a.`state` = 1 AND a.`driver_id` = b.`user_id` 
       AND b.`vehicle_type` = '"+ cartype +"'  
 GROUP BY driver_id
Sign up to request clarification or add additional context in comments.

2 Comments

how to modify the db url : static final String DB_URL = "jdbc:mysql://localhost/core";
Read this one, it will help you: stackoverflow.com/questions/8491729/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.