SQL COUNT国籍,拥有多个艺术家和该国籍的艺术家人数

时间:2017-06-21 10:02:55

标签: mysql sql

ARTIST

(FIRSTNAME,LASTNAME,NATIONALITY)

Miro                        Joan                        Spanish                       
Kandinsky                   Wassily                     Russian                       
Klee                        Paul                        German                        
Matisse                     Henri                       French                        
Chagall                     Marc                        French                          
Sargent                     John Singer                 United States                 
Tobey                       Mark                        United States                 
Horiuchi                    Paul                        United States                 
Graves                      Morris                      United States                 
Julio                       Bloxham Smythe              Spanish                       
Basher                      Bigmouth                    Spanish      

我必须列出数据库中代表多位艺术家的所有国籍,以及该国籍的艺术家人数

所以输出应该是

Spanish         3
United states   4
French          2

6 个答案:

答案 0 :(得分:0)

HAVING是你的朋友

SELECT NATIONALITY, COUNT(*)
FROM ARTIST
GROUP BY NATIONALITY
HAVING COUNT(*)>1

答案 1 :(得分:0)

select nationality, 
       count(firstname) as artists -- counts the artists
from mytable
group by nationality -- we group on this, because we want to aggregate the other info
having count(firstname) >1 -- applies a condition AFTER the count

答案 2 :(得分:0)

您可以使用group by子句为每个国籍申请一个群组,然后使用having条款来仅保留拥有多个艺术家的国籍:

SELECT   nationality, COUNT(*)
FROM     artist
GROUP BY nationality
HAVING   COUNT(*) > 1

答案 3 :(得分:0)

使用分组依据

SELECT count(*),`NATIONALITY` 
FROM `ARTIST` 
GROUP BY `NATIONALITY` 
HAVING count(FIRSTNAME) > 1

答案 4 :(得分:0)

使用HAVING是不必要的,简单的GROUP BY会:

SELECT NATIONALITY,COUNT(*) FROM ARTIST GROUP BY NATIONALITY;

答案 5 :(得分:0)

您需要使用的是Count函数。试试https://www.tutorialspoint.com/sql/sql-count-function.htm

我认为你需要的是一般的计数。它很简单:

Count(*)

我认为这个问题之前已被多次询问,以节省时间,下次尝试搜索文档。