求个sql语句.

igelf 2008-03-12 10:52:42
举个例子 下列数据
name age
a1 18
a1 16
a1 17
a3 19
a4 20
我要取出
name age
a1 18
a3 19
a4 20
怎么写啊.
...全文
303 35 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
lucky 2008-03-13
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 igelf 的回复:]
引用 23 楼 zhuyx808 的回复:
SQL code

select a.* from 表 a where age = (select top 1 age from 表 where name = a.name) order by a.name


小梁对了的

可是如果top 1 的age 和下一个的age相同的话,取出的是二行啊.name会重复的.
[/Quote]
用个distinct 不就可以啦
lantu007 2008-03-13
  • 打赏
  • 举报
回复
楼上正确
select distinct name,age from tb
igelf 2008-03-12
  • 打赏
  • 举报
回复
想到了.用小梁的方法.表中加个自增id,就不重复了.
LeeeeSin 2008-03-12
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 csharpvb 的回复:]
举个例子 下列数据
name age
a1 18
a1 16
a1 17
a3 19
a4 20
我要取出
name age
a1 18
a3 19
a4 20
怎么写啊.
-----------
select * from table where age> 17
[/Quote]

查询的时候怎么可能会知道你查出数据的值是多少呢????
igelf 2008-03-12
  • 打赏
  • 举报
回复
说明下,我实际要取的是很多列的数据.不单单是这两列.
LeeeeSin 2008-03-12
  • 打赏
  • 举报
回复
顶上去
win7cc 2008-03-12
  • 打赏
  • 举报
回复
举个例子 下列数据
name age
a1 18
a1 16
a1 17
a3 19
a4 20
我要取出
name age
a1 18
a3 19
a4 20
怎么写啊.
-----------
select * from table where age>17
igelf 2008-03-12
  • 打赏
  • 举报
回复
比如下面数据.
name age
a1 18
a1 18
a1 17
a3 19
a4 20
将取出
name age
a1 18
a1 18
a3 19
a4 20
igelf 2008-03-12
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 zhuyx808 的回复:]
SQL code

select a.* from 表 a where age = (select top 1 age from 表 where name = a.name) order by a.name






小梁对了的
[/Quote]
可是如果top 1 的age 和下一个的age相同的话,取出的是二行啊.name会重复的.
pt1314917 2008-03-12
  • 打赏
  • 举报
回复
吼``
zhuyx808 2008-03-12
  • 打赏
  • 举报
回复


select a.* from 表 a where age = (select top 1 age from 表 where name = a.name) order by a.name




小梁对了的
一品梅 2008-03-12
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 dawugui 的回复:]
既然这样,不用用三楼小梁子转我的帖.
[/Quote]
呵呵,葵花宝典,武林至尊,天下无敌,谁与争峰~
一品梅 2008-03-12
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 igelf 的回复:]
我要取重复name的第一个
[/Quote]
select dinstinct name,age from tb;
dawugui 2008-03-12
  • 打赏
  • 举报
回复
既然这样,不用用三楼小梁子转我的帖.
LeeeeSin 2008-03-12
  • 打赏
  • 举报
回复
OK?????
igelf 2008-03-12
  • 打赏
  • 举报
回复
恩.15楼理解我意思了
lhsxsh 2008-03-12
  • 打赏
  • 举报
回复
select name,max(age) age
from tb
group by name
igelf 2008-03-12
  • 打赏
  • 举报
回复
再打个比方吧.
name email
a1 a@163.com
a1 b@163.com
a2 c@163.com

我要取出
name email
a1 a@163.com
a2 c@163.com
明白?
liangCK 2008-03-12
  • 打赏
  • 举报
回复
--三、按name分组取第一次出现的行所在的数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
yinqi025 2008-03-12
  • 打赏
  • 举报
回复

declare @t table(
name varchar(50),
age int
)

insert into @t
select 'a1',18
union all
select 'a1',16
union all
select 'a1',12
union all
select 'a2',19
union all
select 'a3',20

select a.[name],max(age) as age
from @t a
group by [name]
加载更多回复(15)

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧