2010-09-20 66 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