代码之家  ›  专栏  ›  技术社区  ›  ssc

MySQL跨数据库WHERE子句

  •  0
  • ssc  · 技术社区  · 14 年前

    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,所以支持跨数据库。

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

    SELECT DISTINCT id FROM measurement_values.measurement_value
    

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

    我想这本书提到的计数技巧 Problem with Query Data in a Table 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)
    

    1 回复  |  直到 8 年前
        1
  •  0
  •   codingguy3000    14 年前

    这应该做到:

    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