一条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