表结构  user表和 Fatie表 关联字段userIdfatie表里有authorId(userid),lastEditorId(userid)authorId和lastEditorId为0时,显示 '游客'否则显示姓名在结果显示 作者(名字)和回复者(名字)

解决方案 »

  1.   

    --设user表中姓名列名为 userNameSELECT ISNULL(b.userName,'guest') 作者,ISNULL(c.userName,'guest') 回复者
    FROM fatie a
    LEFT JOIN [user] b
    ON a.authorId = b.userId
    LEFT JOIN [user] c
    ON a.lastEditorId = c.userId
      

  2.   

    select case when authorid=0 and lasteditorid=0 then '游客'  else authorid end from fatie
      

  3.   

    (case when authorId='' then '游客' else authorId end) as authorId,
    (case when lastEditorId='' then '游客' else lastEditorId end) as lastEditorId,不过建议你这个最好还是前台去判断才好。
      

  4.   

    authorid=0  时user表中无userid=0之记录,则 连表时右端无匹配,用isnull处理就可以了,这个0形同虚设。
      

  5.   


    select 
    case when authorId = 0  and lastEditorId = 0 then
    '游客'
        else
    u1.user_name
    end 作者,
    case when authorId = 0  and lastEditorId = 0  then
    '游客'
        else
    u2.user_name
    end 回复者
    from fatie
    left outer join [user] u1 on u1.userid = fatie.authorId
    left outer join [user] u2 on u2.userid = fatie.lastEditorId
      

  6.   


    不对啊,authorId 和 lastEditorId 都是bigInt型的,和userid类型一样啊isnull好象不能判断bigInt型的吧
      

  7.   

    再加ISNULL判断select 
    case when isnull(authorId,0) = 0  and isnull(lastEditorId,0) = 0 then
    '游客'
        else
    u1.user_name
    end 作者,
    case when isnull(authorId,0) = 0  and isnull(lastEditorId,0) = 0  then
    '游客'
        else
    u2.user_name
    end 回复者
    from fatie
    left outer join [user] u1 on u1.userid = fatie.authorId
    left outer join [user] u2 on u2.userid = fatie.lastEditorId
      

  8.   


    试过才知道。而且我处理是用户名并不是userid.另外,isnull你看下联机丛书就知道它能不能用了。
      

  9.   

    declare @fatie table( authorId bigint,lastEditorId bigint)
    insert @fatie select 1,3
    insert @fatie select 4,2
    declare @user table(userid bigint,username nvarchar(100))
    insert @user select 1,'張三'
    insert @user select 2,'李四' 
    SELECT ISNULL(b.userName,'遊客') 作者,ISNULL(c.userName,'遊客') 回复者
        FROM @fatie a
    LEFT JOIN @user b
        ON a.authorId = b.userId
    LEFT JOIN @user c
        ON a.lastEditorId = c.userId
    /*
    作者                                                                                                   回复者                                                                                                  
    ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- 
    張三                                                                                                   遊客
    遊客                                                                                                   李四*/