如何只取出一条规定条件相同取ID号大的数据

浮生若梦_平淡为真 2012-07-08 10:16:26
有一表比如

ID H T V
1 1 1 1
2 1 1 2
1 2 1 2
1 3 2 1
2 3 3 3
2 2 1 4

如何通过SQL语句 取出数据 要求 如何 H 和 T 都相同 并且 ID 不同的情况下 只取出ID大的数据
...全文
156 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
感谢您,我自己解决了
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
引用 6 楼 的回复:
SQL code
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[H] int,[T] int,[V] int)
insert [tb]
select 1,1,1,1 union all
select 2,1,1,2 union all
selec……
[/Quote]
你好,感谢您的指导,但是我测试了一下,如果只是2个用户就可以,但是如果用户很多,而我又需要用户只能取出自己的数据和ID=1的数据,但是这样不论哪个用户取出的都是ID最大的数据
百年树人 2012-07-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
SQL code
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[H] int,[T] int,[V] int)
insert [tb]
select 1,1,1,1 union all
select 2,1,1,2 union all
select 1,2,1,2 uni……
这个也要写吗?
[/Quote]

这个是根据你给的测试数据建立的测试表和插入数据的语句,查询的时候不需要写,只需要写下面的select开始的查询语句即可
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[H] int,[T] int,[V] int)
insert [tb]
select 1,1,1,1 union all
select 2,1,1,2 union all
select 1,2,1,2 union all
select 1,3,2,1 union all
select 2,3,3,3 union all
select 2,2,1,4
go


这个也要写吗?
百年树人 2012-07-08
  • 打赏
  • 举报
回复
--方法二
select * from tb t
where id=(select max(id) from tb where h=t.h and t=t.t)
/**
ID H T V
----------- ----------- ----------- -----------
2 1 1 2
1 3 2 1
2 3 3 3
2 2 1 4

(4 行受影响)
**/

--方法三
select * from tb t
where id=(select top 1 id from tb where h=t.h and t=t.t order by id desc)
/**
ID H T V
----------- ----------- ----------- -----------
2 1 1 2
1 3 2 1
2 3 3 3
2 2 1 4

(4 行受影响)
**/

--方法四
select a.* from tb a
join (select max(id) as id,h,t from tb group by h,t) b
on a.h=b.h and a.t=b.t and a.id=b.id
/**
ID H T V
----------- ----------- ----------- -----------
2 1 1 2
1 3 2 1
2 3 3 3
2 2 1 4

(4 行受影响)
**/
百年树人 2012-07-08
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[H] int,[T] int,[V] int)
insert [tb]
select 1,1,1,1 union all
select 2,1,1,2 union all
select 1,2,1,2 union all
select 1,3,2,1 union all
select 2,3,3,3 union all
select 2,2,1,4
go

select * from tb t
where not exists(select 1 from tb where h=t.h and t=t.t and id>t.id)
/**
ID H T V
----------- ----------- ----------- -----------
2 1 1 2
1 3 2 1
2 3 3 3
2 2 1 4

(4 行受影响)
**/
eaglerxa 2012-07-08
  • 打赏
  • 举报
回复
select * FROM table as T
left outer join
(select Max(a.id) as id ,a.h,a.t froM table as a
where (select * FROM table as b where a.h=b.h and a.t=b.t and a.id<>b.id )
group by a.h,a.t) as C on T.h = c.h and T.T = C.T
eaglerxa 2012-07-08
  • 打赏
  • 举报
回复
select * FROM table as T
left outer jion
(select Max(a.id) as id ,a.h,a.t froM table as a
where (select * FROM table as b where a.h=b.h and a.t=b.t and a.id<>b.id )
group by a.h,a.t) as C
where T.h = c.h and T.T = C.T
  • 打赏
  • 举报
回复
取出来的 数据 应该是
ID H T V
2 1 1 2
1 3 2 1
2 3 3 3
2 2 1 4

34,590

社区成员

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

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