Sql查询帮助初学者

And*_*eas 1 sql

嗨,我需要一个查询帮助(是的,我一直在尝试搜索,但找不到帮助我的东西)

我有两张桌子:

顾客

  • CustomerID
  • CountryID

国家

  • CountryID
  • Country

我如何编写查询以获得如下结果:

CountryID, Country, NumberOfOccurancesOfThisCountryInTheCustomerTable
Run Code Online (Sandbox Code Playgroud)

真的很感激帮助!

Yuc*_*uck 6

试试这个:

SELECT CountryID, Country,
  COUNT(Customer.CustomerID) AS NumberOfOccurancesOfThisCountryInTheCustomerTable
FROM Country LEFT JOIN
     Customer ON Country.CountryID = Customer.CountryID
GROUP BY Country.CountryID, Country.Country
Run Code Online (Sandbox Code Playgroud)

编辑:使用LEFT JOINvs. INNER JOIN包括Country记录为零的Customer记录(感谢Mark Ba​​nnister).