当前位置: 代码迷 >> Sql Server >> 帮小弟我
  详细解决方案

帮小弟我

热度:83   发布时间:2016-04-27 19:16:03.0
在线等大虾帮我
有2张表 Country表和City表
create table Country
(
Country_code varchar(10),
Country_Name Varchar(20),
)
insert into Country values('CN','China'),
('US','U,S,A'),
('CA','Canada')
create table City
(
City_Code varchar(10),
City_Name Varchar(20),
Country_Code Varchar(10),
constraint PK_City Primary Key(City_Code)
)
insert into City values('BJ','BeiJin','CN'),
('SH','ShangHai','CN'),
('SZ','ShenZhen','CN'),
('BG','baiGong','US'),
('HSD','HuaShengDun','US'),
('DLD','DuoLunDuo','CA')
问:选择出城市个数大于1的国家名称? 小弟菜鸟求救啊! 如上面结果应该是China和U,S,A 在线等啊!

------解决方案--------------------
SQL code
select   a.Country_Namefrom   Country a join City bon   a.Country_code=b.Country_code group by   a.Country_Namehaving   count(1)>1
------解决方案--------------------
SQL code
create table Country(Country_code varchar(10),Country_Name Varchar(20),)insert into Country values('CN','China'),('US','U,S,A'),('CA','Canada')create table City(City_Code varchar(10),City_Name Varchar(20),Country_Code Varchar(10),constraint PK_City Primary Key(City_Code))insert into City values('BJ','BeiJin','CN'),('SH','ShangHai','CN'),('SZ','ShenZhen','CN'),('BG','baiGong','US'),('HSD','HuaShengDun','US'),('DLD','DuoLunDuo','CA')goselect a.country_name from Country a inner join City b on a.Country_code=b.Country_Code group by a.country_namehaving COUNT(*)>1/*country_name--------------------ChinaU,S,A(2 行受影响)*/godrop table city,Country
------解决方案--------------------
SQL code
select*from  Country  as a where exists(select 1 from City where Country_code=a.Country_code having count(1)>1)
  相关解决方案