SELECT TOP 20 * from table ORDER BY ID DESC, totop DESC
我要的是最新的20条数据里面 按TOTOP 来排序,这个要怎么写 这里ID 是唯一值, totop 是0 或是1 就是要让最新的20条里面totop 是1 的排前面。这个要怎么写

解决方案 »

  1.   

    SELECT TOP 20 * from table ORDER BY totop DESC
      

  2.   

    这个ID 是唯一值,就按ID排了,不会再按后面的TOTOP来排序了
      

  3.   

    SELECT TOP 20 * from table where totop=1 ORDER BY ID DESC
      

  4.   

    SELECT TOP 20 * from table ORDER BY totop DESC,ID DESC
      

  5.   

    SELECT TOP 20 * from table 
    ORDER BY 
      totop DESC, --先把1的排前面
      ID DESC  --按id大小取前20个
      

  6.   


    --运行可以直接看结果
    create table table1(id int,totop int, name varchar(255) )
    go
    insert into table1(id,totop,name)values(0,0,'小明0')
    insert into table1(id,totop,name)values(1,1,'小明1')
    insert into table1(id,totop,name)values(2,0,'小明2')
    insert into table1(id,totop,name)values(3,1,'小明3')
    insert into table1(id,totop,name)values(4,0,'小明4')
    insert into table1(id,totop,name)values(5,1,'小明5')
    insert into table1(id,totop,name)values(6,0,'小明6')
    insert into table1(id,totop,name)values(7,1,'小明7')
    insert into table1(id,totop,name)values(8,0,'小明8')
    insert into table1(id,totop,name)values(9,1,'小明9')
    go
    --用子查询方法实现,注意这里用了别名
    select * 
    from (SELECT TOP 5 * from table1 ORDER BY ID DESC ) A --别名
    order by A.totop DESC
    go
    drop table table1
    go
      

  7.   

    郁闷还没解决问题,,,SELECT TOP 20 * from table ORDER BY totop DESC,ID DESC这个出来都是totop=1的值了。要的是 SELECT TOP 20 * from table ORDER BY ID DESC 这里的数据读出来后再按 totop 来排序,要的是最新的20数据的排序,