日期:2014-05-18 浏览次数:20743 次
select b.titlecode from Table1 as b where b.dirname= '芙蓉姐姐的故事 ' and exists ( select top 1 a.titleid from Table1 as a where a.dirname= '芙蓉姐姐的故事 ' and a.titleid >b.titleid
------解决方案--------------------
select top 1
b.titlecode
from Table1 as b
where b.dirname= '芙蓉姐姐的故事'
and exists (select *
from Table1
where dirname= '芙蓉姐姐的故事' and titleid >b.titleid )
set nocount on create table table1 ( TitleID int Identity(1,1) not null, Title varchar(200) null, TitleCode uniqueidentifier null, DirName varchar(100) null, PagePre uniqueidentifier null ) insert table1 (TitleCode, DirName) select newID(), 'FRJJ''s story' union all select newID(), 'FRMM''s story' union all select newID(), 'FRGG''s story' union all select newID(), 'FRDD''s story' union all select newID(), 'FRJJ''s story' union all select newID(), 'FRMM''s story' union all select newID(), 'FRGG''s story' union all select newID(), 'FRDD''s story' union all select newID(), 'FRJJ''s story' union all select newID(), 'FRMM''s story' union all select newID(), 'FRGG''s story' union all select newID(), 'FRDD''s story' GO update table1 set PagePre = (select titlecode from table1 where DirName = t.DirName AND titleid = (select max(titleID) from table1 where DirName = t.DirName AND titleid < t.titleid)) from table1 t select * from table1 GO drop table table1 GO
------解决方案--------------------
刚才在.net版玩,没看到...
借用 Generics 的测试数据
我将字段改了一下,方便我看替换后的结果是否正确.
create table table1 ( TitleID int Identity(1,1) not null, Title varchar(200) null, TitleCode varchar null, DirName varchar(100) null, PagePre varchar null ) insert table1 (TitleCode, DirName) select 'a', 'FRJJ''s story' union all select 'b', 'FRMM''s story' union all select 'c', 'FRGG''s story' union all select 'd', 'FRDD''s story' union all select 'e', 'FRJJ''s story' union all select 'f', 'FRMM''s story' union all select 'g', 'FRGG''s story' union all select 'h', 'FRDD''s story' union all select 'i', 'FRJJ''s story' union all select 'j', 'FRMM''s story' union all select 'k', 'FRGG''s story' union all select 'l', 'FRDD''s s