SQLite查询正在挂起

时间:2017-02-01 17:51:09

标签: sqlite

所以我有一个名为运动员的桌子,有五个领域:运动员,运动,金,银,铜。从这张表中我们创建了一个名为most_played_sports的视图,其中包含拥有超过500名运动员的所有体育项目。手头的任务是使用上述视图找到不同的运动对(sport_1,sport_2),以使sport_1的奖牌数量严格小于sport_2。

我试过的查询是:

create view if not exists most_played_sports as select sport, sum(gold)+sum(silver)+sum(bronze) as medal_count from athletes
    group by sport
    having count(id) >= 500;

select distinct athletes.sport as sport_1, most_played_sports.sport as sport_2 
    from athletes cross join most_played_sports
    where sport_1 > sport_2 and 
    ((select medal_count from most_played_sports where sport=sport_1) < (select medal_count from most_played_sports where sport=sport_2));

第二个查询返回一些结果,但是挂起了。有没有比我尝试过的更好的方法来完成这项任务?

1 个答案:

答案 0 :(得分:1)

我想你想要一个自我加入:

select mps.sport as sport_1, mps2.sport as sport_2
from most_played_sports mps join
     most_played_sports mps2
     on mps.medal_count < mps2.medal_count;

这个问题并不表示需要使用原始表。