2017-08-24 75 views
-1

我有一个给定格式的mysql表。在这里,我有一个城市和一个参数,如果城市里有足球场,这个参数就是真实的。我想找到每个大洲至少有2个城市拥有足球场的国家的比例。MYSQL:查找符合条件的行数

Id | Stadium| City  | Country | Continent 
__________________________________________________ 
1 | true | Manchester | UK  | Europe 

2 | true | London  | UK  | Europe 

3 | false | Leeds  | UK  | Europe 

4 | true | Berlin  | Germany | Europe 

5 | false | Dubai  | Dubai | Asia 

我是MySQL新手。我无法弄清楚如何做到这一点。有人可以请帮助。

回答

1

您可以使用子查询,并做一些这样的:

SELECT q.Continent as continent, SUM(IF(q.qntStadiuns > 2, 1, 0))/COUNT(*) * 100 as percent 
FROM 
    (SELECT Continent, Country, COUNT(*) as qntStadiuns 
    FROM tableName 
    WHERE Stadium = true 
    GROUP BY Continent, Country) AS q 
GROUP BY continent