本帖最后由 decher 于 2010-02-09 17:31:08 编辑

解决方案 »

  1.   

    --> 测试数据:[Information]
    if object_id('[Information]') is not null drop table [Information]
    create table [Information]([ID] varchar(1),[State] int)
    insert [Information]
    select 'a',0 union all
    select 'b',1 union all
    select 'c',1 union all
    select 'd',1 union all
    select 'e',0
    --> 测试数据:[InformationComment]
    if object_id('[InformationComment]') is not null drop table [InformationComment]
    create table [InformationComment]([ID] varchar(3),[InformationID] varchar(1),[Content] varchar(8))
    insert [InformationComment]
    select 'I','a','回复内容' union all
    select 'II','a','回复内容' union all
    select 'III','c','回复内容' union all
    select 'IV','b','回复内容' union all
    select 'V','c','回复内容'select b.[id],b.[InformationID],a.[State],b.[Content]  from [Information] a
    left join [InformationComment] b 
    on a.[id]=b.InformationID    where a.[State]=1
    and not exists (select 1 from [InformationComment] where [InformationID]=b.[InformationID] and [id]<b.[id])
    /*
    id   InformationID State       Content
    ---- ------------- ----------- --------
    IV   b             1           回复内容
    III  c             1           回复内容
    NULL NULL          1           NULL(3 行受影响)*/
      

  2.   

    --> 测试数据:[Information]
    if object_id('[Information]') is not null drop table [Information]
    create table [Information]([ID] varchar(1),[State] int)
    insert [Information]
    select 'a',0 union all
    select 'b',1 union all
    select 'c',1 union all
    select 'd',1 union all
    select 'e',0
    --> 测试数据:[InformationComment]
    if object_id('[InformationComment]') is not null drop table [InformationComment]
    create table [InformationComment]([ID] varchar(3),[InformationID] varchar(1),[Content] varchar(8))
    insert [InformationComment]
    select 'I','a','回复内容' union all
    select 'II','a','回复内容' union all
    select 'III','c','回复内容' union all
    select 'IV','b','回复内容' union all
    select 'V','c','回复内容'select b.[id],a.[id]as InformationID,a.[State],b.[Content]  from [Information] a
    left join [InformationComment] b 
    on a.[id]=b.InformationID    where a.[State]=1
    and not exists (select 1 from [InformationComment] where [InformationID]=b.[InformationID] and [id]<b.[id])
    order by b.[id] desc
    /*
    id   InformationID State       Content
    ---- ------------- ----------- --------
    IV   b             1           回复内容
    III  c             1           回复内容
    NULL d             1           NULL
    (3 行受影响)*/
      

  3.   

    --> 测试数据:[Information] 
    if object_id('[Information]') is not null drop table [Information] 
    create table [Information]([ID] varchar(1),[State] int) 
    insert [Information] 
    select 'a',0 union all 
    select 'b',1 union all 
    select 'c',1 union all 
    select 'd',1 union all 
    select 'e',0 
    --> 测试数据:[InformationComment] 
    if object_id('[InformationComment]') is not null drop table [InformationComment] 
    create table [InformationComment]([ID] varchar(3),[InformationID] varchar(1),[Content] varchar(8)) 
    insert [InformationComment] 
    select 'I','a','回复内容' union all 
    select 'II','a','回复内容' union all 
    select 'III','c','回复内容' union all 
    select 'IV','b','回复内容' union all 
    select 'V','c','回复内容' select b.[id],a.[id]as InformationID,a.[State],b.[Content]  from [Information] a 
    left join [InformationComment] b 
    on a.[id]=b.InformationID    where a.[State]=1 
    and not exists (select 1 from [InformationComment] where [InformationID]=b.[InformationID] and [id] <b.[id]) 
    order by b.[id] desc 
    /* 
    id  InformationID State      Content 
    ---- ------------- ----------- -------- 
    IV  b            1          回复内容 
    III  c            1          回复内容 
    NULL d            1          NULL 
    (3 行受影响) */