2010-09-20 70 views
0

我正在从一个项目中获取来自遍布世界各地的许多测量站(例如50000)的值。我有2个数据库,一个存储测量站的信息,另一个存储从这些站获得的值(例如几百万)。数据库结构的超简化的版本看起来是这样的:MySQL跨数据库WHERE子句

database measurement_stations 

    table measurement_station 
    id  : primary key 
    name : colloquial station name 
    country : foreign key into table country 

    table country 
    id  : primary key 
    name : name of the country 

database measurement_values 

    table measurement_value 
    id  : primary key 
    station : id of the station the value came from 
    value : measured value 

我需要从第一数据库,其在第二数据库中值的所有国家名称的列表。我在InnoDB中使用MySQL,因此支持跨数据库外部。

我迷失于SELECT语句,更具体地说,where子句。

选择其值存在于国家的ID似乎很容易:

SELECT DISTINCT id FROM measurement_values.measurement_value 

这需要一对夫妇首次分钟,但实在是快在随后的电话,甚至在数据库服务器重新启动;我认为这很正常。

我认为在Problem with Query Data in a TableMysql Complex Where Clause中提到的COUNT技巧可能会有所帮助,但我似乎无法解决问题。

SELECT country.name FROM measurement_stations WHERE country.id = measurement_station.id 
AND (id is in the result of the previous SELECT statement) 

任何人都可以帮助我吗?

回答

0

这应做到:

select distinct m.country, ct.name 
from measurement_stations..measurement_station m 
inner join measurement_values..measurement_value mv on mv.station = m.id 
inner join measurement_stations..country ct on ct.id = m.country 
+0

这样的作品,优秀的。非常感谢您的快速帮助! :-) – ssc 2010-09-21 04:01:11

+0

我第一次运行查询:集合中的184行(27分10.41秒) – ssc 2010-09-21 04:01:39

+0

第二次我运行它:184行集(1分20.92秒) – ssc 2010-09-21 04:02:14