嗨,我需要一个查询帮助(是的,我一直在尝试搜索,但找不到帮助我的东西)
我有两张桌子:
顾客
CustomerIDCountryID国家
CountryIDCountry我如何编写查询以获得如下结果:
CountryID, Country, NumberOfOccurancesOfThisCountryInTheCustomerTable
Run Code Online (Sandbox Code Playgroud)
真的很感激帮助!
试试这个:
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 Bannister).