按SQL语句分组-获取超过5个城市的国家/地区
我正试图吸引超过5个城市的国家 表: 城市按SQL语句分组-获取超过5个城市的国家/地区,sql,sql-server,Sql,Sql Server,我正试图吸引超过5个城市的国家 表: 城市城市id、城市、国家id、上次更新 国家国家id、国家、上次更新 我想我很快就能明白这一点,但我还没完全明白。有什么建议吗 SELECT DISTINCT country FROM country C, city O WHERE O.country_id = C.country_id AND O.country_id IN (SELECT country_id FROM city group by country_id having count(cou
城市id、城市、国家id、上次更新
国家国家id、国家、上次更新
我想我很快就能明白这一点,但我还没完全明白。有什么建议吗
SELECT DISTINCT country
FROM country C, city O
WHERE O.country_id = C.country_id AND O.country_id
IN (SELECT country_id FROM city group by country_id having count(country_id) > 5);
你可以这样试试
select countryid, count(distinct city_id) from country c join city ct c.country_id=ct.country_id
group by countryid
having count(distinct city_id) > 5
下面的查询适用于您的要求
SELECT C.country
FROM country C
INNER JOIN
city O
ON O.country_id = C.country_id
GROUP BY C.country
HAVING count(distinct O.city)>5;
你应该读这篇文章。使用计数(城市)以防其为空+但是我给了我1张。是的,OP没有具体说明。您甚至可能需要使用
count(distinct city)
以防出现重复点。。。不管怎样,这对他们来说可能更好(不同的是)
select countryid, count(distinct city_id) from country c join city ct c.country_id=ct.country_id
group by countryid
having count(distinct city_id) > 5
SELECT DISTINCT
country
FROM country C
WHERE EXISTS (SELECT COUNT(DISTINCT city_id)
FROM city CITY
WHERE CITY.country_id = C.country_id
GROUP BY CITY.country_id
HAVING COUNT(DISTINCT city_id) > 5);
SELECT C.country
FROM country C
INNER JOIN
city O
ON O.country_id = C.country_id
GROUP BY C.country
HAVING count(distinct O.city)>5;