1

我有一个包含航空公司路线数据的数据库。其中一些属性是sources_airport_ID、destination_airport_ID 和airline(支持从源到目的地的路线)。我正在尝试自行加入表格以查找具有相同路线的航空公司(换句话说,相同的sources_airport_ID 和destination_airport_ID)。

我使用的查询如下:(表名=路由)

SELECT t1.*, t2.*
  FROM routes AS t1, routes AS t2
 WHERE t1.sources_airport_ID = t2.sources_airport_ID 
   AND t1.destination_airport_ID = t2.destination_airport_ID 
   AND t1.airline != t2.airline

当我执行此查询时,我收到一条错误消息,指出最大执行时间超过 300 秒。我不确定我是否为此目的使用了正确的查询。谁能帮我查询?我使用 xampp 作为我的数据库。

提前致谢!

编辑:我的主键是 ID,它只是一个自动增量值。路由表中有 64,114 条记录。

4

2 回答 2

1

尝试使用 JOIN 语法:

SELECT t1.*, t2.*
  FROM routes AS t1
  JOIN routes AS t2
  ON   t1.sources_airport_ID = t2.sources_airport_ID 
  AND  t1.destination_airport_ID = t2.destination_airport_ID 
  AND  t1.airline != t2.airline

但正如建议的那样,请确保字段sources_airport_ID, destination_airport_IDairline在路由表中被索引。

于 2011-02-28T22:25:46.033 回答
0

尝试这样的事情:

SELECT r.*, count(r.sources_airport_ID) as occ 
FROM routes r 
GROUP BY sources_airport_ID, destination_airport_ID, airline
HAVING occ > 1;
于 2011-02-28T22:39:24.710 回答