首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

一条SQL查询语句不会写,请大家帮帮忙!解决思路

2012-01-18 
一条SQL查询语句不会写,请大家帮帮忙!表User中有字段UserAge,int类型怎样把表中只要有年龄一样的用户都选

一条SQL查询语句不会写,请大家帮帮忙!
表User中有字段UserAge,int类型
怎样把表中只要有年龄一样的用户都选出来?(不指定年龄大小)
比如:
20
20
21
21
35
35
35

[解决办法]
select *
from User a
where exists (select 1 from User where userid <> a.userid and UserAge = a.UserAge)

[解决办法]
http://community.csdn.net/Expert/topic/5275/5275251.xml?temp=.1500818

帮我看看哈.
[解决办法]
Insert Into [User] Select 'John1 ', 20 Union All
Select 'John2 ', 20 Union All
Select 'John3 ', 21 Union All
Select 'John4 ', 21 Union All
Select 'John5 ', 35 Union All
Select 'John6 ', 35 Union All
Select 'John7 ', 35

Select UserName From [User] Where UserAge In(Select UserAge From [User] Group By UserAge Having Count(UserAge) > 1)
/*
UserName
--------------------
John1
John2
John3
John4
John5
John6
John7

(所影响的行数为 7 行)

*/

Drop Table [User]
[解决办法]
--try

select * from [User] where UserAge in
(select UserAge from [User] group by UserAge having count(*)> 1)
[解决办法]
如果不查别的数据,这句就行了
select UserAge from [User] group by UserAge having count(UserAge) > 1
要加上用户信息:
select * from [User] a inner join (select UserAge from [User] group by UserAge having count(UserAge) > 1)tem on tem.UserAge= a.UserAge

热点排行