MySQL 跨数据库 WHERE 子句

发布于 2024-09-24 08:10:07 字数 1301 浏览 3 评论 0原文

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

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

我需要第一个数据库中所有国家/地区名称的列表,这些国家/地区的值存在于第二个数据库中。我使用的是MySQL和InnoDB,所以支持跨数据库的foreign。

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

选择存在值的国家/地区的 ID 似乎很容易:

SELECT DISTINCT id FROM measurement_values.measurement_value

第一次需要几分钟,但在后续调用中速度非常快,即使在数据库服务器重新启动后也是如此;我认为这很正常。

我认为 表中查询数据的问题中提到的 COUNT 技巧和 Mysql Complex Where Clause 可以提供帮助,但我似乎无法正确理解。

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

谁能帮助我吗?

I am working on a project that obtains values from many measurement stations (e.g. 50000) located all over the world. I have 2 databases, one storing information on the measurement stations, the other one storing values obtained from these stations (e.g. several million). A super-simplified version of the database structure could look like this:

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

I need a list of the names of all countries from the first database for which values exist in the second database. I am using MySQL with InnoDB, so cross-database foreign are supported.

I am lost on the SELECT statement, more specifically, the where clause.

Selecting the IDs of the countries for which values exist seems easy:

SELECT DISTINCT id FROM measurement_values.measurement_value

This takes a couple of minutes the first time, but is really fast in subsequent calls, even after database server restarts; I assume that's normal.

I think the COUNT trick mentioned in Problem with Query Data in a Table and Mysql Complex Where Clause could help, but I can't seem to get it right.

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

Can anyone help me ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪都笑了 2024-10-01 08:10:07

这应该可以做到:

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

This should do it:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文