Select MySQL跨数据库WHERE子句

Select MySQL跨数据库WHERE子句,select,mysql,where-clause,Select,Mysql,Where Clause,我正在从事一个项目,该项目从世界各地的许多测量站获得数值,例如50000个。我有两个数据库,一个存储测量站的信息,另一个存储从这些站获得的值,例如几百万。数据库结构的超级简化版本可能如下所示: database measurement_stations table measurement_station id : primary key name : colloquial station name country : foreign key in

我正在从事一个项目,该项目从世界各地的许多测量站获得数值,例如50000个。我有两个数据库,一个存储测量站的信息,另一个存储从这些站获得的值,例如几百万。数据库结构的超级简化版本可能如下所示:

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
第一次这样做需要几分钟,但在后续调用中速度非常快,即使在数据库服务器重新启动之后也是如此;我想这是正常的

我认为和中提到的计数技巧可能会有所帮助,但我似乎做得不对

SELECT country.name FROM measurement_stations WHERE country.id = measurement_station.id
AND (id is in the result of the previous SELECT statement)
有人能帮我吗?

这应该可以:

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

很好,很好。非常感谢您的快速帮助!:-第一次运行查询:在27分钟10.41秒的时间内运行184行第二次运行:在1分钟20.92秒的时间内运行184行关系数据库管理系统SQL Server、MySQL、Oracle等将缓存查询,以便在第二次运行时提高性能。这可能会在生产环境中导致问题。您应该考虑添加索引,以解决初始查询运行时的性能问题。