A表:
id Stid isUser time
---------------------------------------------------------
64 86 115 2009/5/16 3:31:39 ffdf
65 85 115 2009/5/16 3:31:43 sdfsdf
66 87 115 2009/5/16 3:34:37 dasdasd
67 88 115 2009/5/16 3:38:59 dsd
68 89 115 2009/5/16 3:40:06 fff
69 90 115 2009/5/16 3:51:49 rerew
70 90 115 2009/5/16 3:51:58 rff
71 91 115 2009/5/16 3:52:53 333
72 91 115 2009/5/16 3:53:45 111
73 92 115 2009/5/16 3:54:06 asss
74 93 115 2009/5/16 3:56:37 333
75 94 115 2009/5/16 3:56:56 333
---------------------------------------------------B表
----------------------------------------------------
84 关羽
85 张飞
86 赵云
87 刘备
88 fdsfsdfsdf
89 111
90 5555
91 333
92 sdasd
93 333
94 21211
95 赵云
----------------------------------------------------
B表的id=A表的STID
要求查询此表中A表中STID是重复的,如果重复一次(也就是求出count(*)=1)并且 A表中此条的time小于当前时间那么循环出此条数据,如果重复第二次(也就是求出count(*)=2),max求出最后一次出现那个ID,并且 A表中此条的time小于当前时间那么再循环出此条数据,以此类推
id Stid isUser time
---------------------------------------------------------
64 86 115 2009/5/16 3:31:39 ffdf
65 85 115 2009/5/16 3:31:43 sdfsdf
66 87 115 2009/5/16 3:34:37 dasdasd
67 88 115 2009/5/16 3:38:59 dsd
68 89 115 2009/5/16 3:40:06 fff
69 90 115 2009/5/16 3:51:49 rerew
70 90 115 2009/5/16 3:51:58 rff
71 91 115 2009/5/16 3:52:53 333
72 91 115 2009/5/16 3:53:45 111
73 92 115 2009/5/16 3:54:06 asss
74 93 115 2009/5/16 3:56:37 333
75 94 115 2009/5/16 3:56:56 333
---------------------------------------------------B表
----------------------------------------------------
84 关羽
85 张飞
86 赵云
87 刘备
88 fdsfsdfsdf
89 111
90 5555
91 333
92 sdasd
93 333
94 21211
95 赵云
----------------------------------------------------
B表的id=A表的STID
要求查询此表中A表中STID是重复的,如果重复一次(也就是求出count(*)=1)并且 A表中此条的time小于当前时间那么循环出此条数据,如果重复第二次(也就是求出count(*)=2),max求出最后一次出现那个ID,并且 A表中此条的time小于当前时间那么再循环出此条数据,以此类推
解决方案 »
- Sql 语句 各位帮帮忙!!!!!
- ASP.NET弹出带有“是”和“否”的对话框问题(在线等!急)
- 页面优化,数据压缩,怎样传输数据量?
- 如何在.net中让oracle只能允许一个连接?
- Asp.net页面的生命周期究竟有那些?
- asp问题
- 在DATAGRID绑定数据的时候,如果个列的数据满足一个条件的话,将这一行的背景色改为红色!应该怎么实现?
- 定义为protected是不是全部可以定义为Public
- repeater嵌套子repeater的循环绑定
- 在asp.net中,各位是怎样共用一些页面?
- 问一个小问题:网站仅有一个用户登录,SQL数据库怎么却显示有5个连接?
- 有人知道校内开心农场自动杀虫除草偷窃的外挂如何实现?
这个意思?
SELECT Stid from table1 a where Stid = (select top 1 id from table1 where stid=a.stid order by stid desc ) AND datadiff(minute,convert(carchar(20),time,120),convert(carchar(20),getdate(),120) )=0 )
楼主昨天已经在MS-sql版发过了,都不明白楼主的意思
------------
看起来有些累
declare @t table(ID int,Stid int,isUser int,stime datetime)
insert into @t
select 64,86,115,'2009/5/16 3:31:39'
union all
select 65,85,115,'2009/5/16 3:31:43'
union all
select 66,87,115,'2009/5/16 3:34:37'
union all
select 67,88,115,'2009/5/16 3:38:59'
union all
select 68,89,115,'2009/5/16 3:40:06'
union all
select 69,90,115,'2009/5/16 3:51:49'
union all
select 70,90,115,'2009/5/16 3:51:58'
union all
select 71,91,115,'2009/5/16 3:52:53'
union all
select 72,91,115,'2009/5/16 3:53:45'
union all
select 73,92,115,'2009/5/16 3:54:06'
union all
select 74,93,115,'2009/5/16 3:56:37'
union all
select 75,94,115,'2009/5/16 3:56:56'select * from @t where ID in(select Max(ID) from @t where stime<getdate() group by Stid )
ID Stid isUser stime
----------- ----------- ----------- ------------------------------------------------------
64 86 115 2009-05-16 03:31:39.000
65 85 115 2009-05-16 03:31:43.000
66 87 115 2009-05-16 03:34:37.000
67 88 115 2009-05-16 03:38:59.000
68 89 115 2009-05-16 03:40:06.000
70 90 115 2009-05-16 03:51:58.000
72 91 115 2009-05-16 03:53:45.000
73 92 115 2009-05-16 03:54:06.000
74 93 115 2009-05-16 03:56:37.000
75 94 115 2009-05-16 03:56:56.000(所影响的行数为 10 行)
这个条件描述的不清楚,重复一次的意思应该是有两条记录才算重复啊如果按我的理解就是重复的记录取ID为最大的并且[time]小于当前时间的记录
如果那样的话还要加上having(count(Stid)>1)
select * from @t where ID in(select Max(ID) from @t where stime<getdate() group by Stid having count(Stid)>1 )
如果我理解错了那就用上面的,也就是不带having的那个,如果没错应该用这个