数据库表如下A:
ID(自增)          喂食次数
-------------------------------
1                   1
2                   1
3                   2
4                   1
5                   6
6                   3
7                   4
8                   2
9                   5现在我要这样查出来(条件有先后)
先按喂食次数按升排列,然后按ID升排列,并每次最多只取前面的10个
ID就相当于是排队的一个次序,也就是每次查出来的10个数据是喂食次数最少的,排在前面的,(喂食次数首先考虑)

解决方案 »

  1.   

    select top 10 * from a order by 喂食次数 ,id
      

  2.   

    select * from table_name as t where ID in (select top 10 ID from table_name where 喂食次数=t.喂食次数 order by ID)
      

  3.   

    insert into #tab(喂食次数)  select 1 union all select 1 union all select 2 union  select 1 union all select 6 union all select 3 union all select 3
    union all select 4 union all select 2 union all select 5select top 10 喂食次数,id from #tab a order by 喂食次数,id
    喂食次数        id
    ----------- -----------
    1           1
    2           2
    2           7
    3           4
    3           5
    4           6
    5           8
    6           3(8 行受影响)
    如果有相同次数 都要显示select top 10 with ties  喂食次数,id from #tab a order by 喂食次数,id
      

  4.   

    select top 10 * from a order by 喂食次数 ,id
    这样就可以了,