2017-08-24 57 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