MySQL 跨数据库 WHERE 子句
我正在开展一个项目,该项目从世界各地的许多测量站(例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以做到:
This should do it: