想在查找字段前加上一递增列,此递增列数据库里面没有,能否在select语句里面实现呢?

解决方案 »

  1.   

    select identity(int,1,1) as id,*
    into #t_1
    from 表select * from #t_1
      

  2.   

    select id1=identity(int,1,1),* into #t from a
    select * from #t
      

  3.   

    参考:create table Test
    (
    A_id   int,
    title  varchar(10),
    num    int,
    LastDT datetime
    )
    insert Test(A_id,title,num,LastDT)
    select 1,'吃饭',80,'2006-8-24 10:37:24' union all
    select 2,'睡觉',75,'2006-8-26 09:55:09' union all
    select 3,'看书',80,'2006-8-25 10:37:24' union all
    select 4,'上网',92,'2006-8-28 11:39:25' union all
    select 5,'游戏',70,'2006-8-27 11:40:18'
    --select * from Test order by num desc,lastDT-- 如果LastDT日期值相同的话还是会并列第 几 名的
    select *,
    排名=
    (
    select count(*) from Test 
    where num > A.num 
    or (num = A.num and LastDT<=A.LastDT) 

    from Test A
    order by 排名drop table Test
      

  4.   

    我不想用临时表,也不是要排名,只是要有一个递增的ID而已,而且数据库比较复杂,YiZhiNet的做法要写很长的语句,能否可以有简单的方法呢