SELECT dbo.by_content.csubject AS 主题, dbo.by_content.uid AS 发帖人ID, 
dbo.by_content.cstate AS 帖子状态, dbo.by_users_info.uname AS 发帖人Name,isnull(
(SELECT dbo.by_users_info.uname
FROM dbo.by_users_info
WHERE dbo.by_users_info.uid IN
(SELECT TOP 1 dbo.by_recontent.uid
FROM dbo.by_recontent
WHERE dbo.by_recontent.cid= dbo.by_content.cid
ORDER BY rtime DESC))
,by_content.发帖人名称
) AS 最后回复人,isnull(
(SELECT TOP 1 dbo.by_recontent.rtime
FROM dbo.by_recontent 
WHERE dbo.by_recontent.cid = dbo.by_content.cid
ORDER BY rtime DESC)
,by_content.发帖时间
) AS 最后回复时间,(
SELECT ISNULL( SUM(CAST(dbo.by_recontent.rcounter AS INT)), 0) FROM dbo.by_recontent 
WHERE dbo.by_recontent.cid = dbo.by_content.cid
) AS 人气FROM dbo.by_content INNER JOIN
dbo.by_users_info ON dbo.by_content.uid = dbo.by_users_info.uid

解决方案 »

  1.   

    学习
    小弟.NET菜鸟 主搞ASP.NET 希望结识有共同爱好的朋友
    QQ群:35554886 MSN:[email protected]
      

  2.   

    用 isnull 判断,否则去查询发帖人数据
      

  3.   

    try
    --------------------------------
    select 
    a.cSubject as 贴子主题,
    a.uid as 发贴人ID,
    a.cstate as 贴子状态,
    u1.uName as 发贴人Name,
    isNull(u2.uName ,u1.uName) as 最后回复人,
    isNull(b.rtime,a.发贴时间) as 最后回复时间,
    isNull(c.人气,0) as 人气from by_content a
    left join (select top 1 uid,cid,rtime from by_recontent order by rtime desc) b on a.uid=b.uid
    left join by_users_info u1 on a.uid=u1.uid
    left join by_users_info u2 on b.uid=u2.uid
    left join (select cid,sum(isNull(cast(rcounter as int),0)) as 人气 from by_recontent group by cid) c on c.cid=a.cid