本帖最后由 zzhao1979 于 2012-08-04 09:50:52 编辑

解决方案 »

  1.   


    "select * from(select row_number() over(partition by qid,tid order by qid,tid) rn,* from test) t where ((t.qid in(1,2) and t.rn<3) or (t.qid not in(1,2))) and t.qid="&ID&" and t.TID="&TID  ,conn,1,1
      

  2.   

    --> 测试数据:[tb]
    IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]
    GO
    CREATE TABLE [tb]([id] INT,[qid] INT,[tid] INT,[text] VARCHAR(4))
    INSERT [tb]
    SELECT 1,0,3,'asp' UNION ALL
    SELECT 2,1,3,'php' UNION ALL
    SELECT 3,1,3,'aspx' UNION ALL
    SELECT 4,1,3,'js' UNION ALL
    SELECT 5,2,3,'jsp' UNION ALL
    SELECT 6,2,3,'html'
    GO--> 测试语句:
    SELECT * FROM [tb]  as a where (select count(*) from tb where [qid]=a.[qid] and [id]<=a.[id])<=2
    /*
    id          qid         tid         text
    ----------- ----------- ----------- ----
    1           0           3           asp
    2           1           3           php
    3           1           3           aspx
    5           2           3           jsp
    6           2           3           html(5 行受影响)*/是这个样子吗
      

  3.   


    SELECT * FROM [tb]  as a
     where (select count(1) from tb where [qid]=a.[qid] and [id]<=a.[id] 
    and a.[qid]=[id])<=2