如何使用JDBC从两个不同的MySQL数据库中检索数据?

时间:2015-02-13 05:28:45

标签: java mysql jdbc

如何使用jdbc在单个查询中从两个不同的MySQL数据库中检索数据?

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
  • 具有rl_driv_location表格的位置数据库。
  • 具有rl_driver_info表的核心数据库。

1 个答案:

答案 0 :(得分:1)

只需指定要检索数据的数据库的名称。见下文。

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