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小于当前时间那么再循环出此条数据,以此类推

解决方案 »

  1.   

    select count(*) from a,b where a.stid=b.id order by time
      

  2.   

    select count(*) from a,b where a.stid=b.id order by time
      

  3.   

    select * from @tba a where ID=(select max(ID) from @tba where a.Stid=Stid) and  a.time<getdate()
    这个意思?
      

  4.   

    select * from B WHERE ID IN(
    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 )
      

  5.   

    http://topic.csdn.net/u/20090511/23/9515b5b1-a1f9-41d9-b7a4-369aa95ce08c.html
      

  6.   

    http://topic.csdn.net/u/20090511/23/9515b5b1-a1f9-41d9-b7a4-369aa95ce08c.html
      

  7.   


    楼主昨天已经在MS-sql版发过了,都不明白楼主的意思
      

  8.   

    要求查询此表中A表中STID是重复的,如果重复一次(也就是求出count(*)=1)并且 A表中此条的time小于当前时间那么循环出此条数据,如果重复第二次(也就是求出count(*)=2),max求出最后一次出现那个ID,并且 A表中此条的time小于当前时间那么再循环出此条数据,以此类推
    ------------
    看起来有些累
      

  9.   


    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 行)
      

  10.   

    如果重复一次(也就是求出count(*)=1)
    这个条件描述的不清楚,重复一次的意思应该是有两条记录才算重复啊如果按我的理解就是重复的记录取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的那个,如果没错应该用这个