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.
Stefan Hanke
  • 3,458
  • 2
  • 30
  • 34
Innovative Thinker
  • 1,735
  • 2
  • 14
  • 24

1 Answers1

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
Rigel1121
  • 2,022
  • 1
  • 17
  • 24