create table logs
(
userId varchar(16) not null, --用户ID
logId int identity(1,1) not null primary key, --日志ID
logTitle varchar(50) not null, --日志标题
logType varchar(10) not null, --日志类型(日志类型表)
logContent varchar(4000) not null, --日志内容
logTime datetime not null, --发表时间
isCommend varchar(1) --是否推荐
)
create table logType
(
userId varchar(16) not null, --用户ID
logTypeId int identity(1,1) not null primary key, --日志类型ID
logTypeName varchar(20) not null --日志类型名称
)create table comments
(
userId varchar(16) not null, --用户ID
commentId int identity(1,1) not null primary key, --评论ID
commentType varchar(10) not null, --评论类型(0为日志,1为相片,2为音乐)
commentLogId varchar(10), --日志ID
commentAlbumId varchar(10), --相片ID
commentMusicId varchar(10), --音乐ID
commentContent varchar(500) not null, --评论内容
commentAuthor varchar(16) not null, --评论作者
commentTime datetime not null, --评论时间
)表的结构如上,想查询该用户的所有日志的日志标题,日志ID,日志时间,日志类型名,及有多少评论。如果是0条评论也要查出日志,效果像下面的[日志类型]日志标题                            评论(0)
[日志类型]日志标题                            评论(5)

解决方案 »

  1.   

    select distinct a.logTitle,a.logTitle,a.logTime,b.logTypeName,count(c.*)
    from logs a,logType b,outer comments c
    where a.userId = 'xxxxxx'
    and a.userId = b.userId
    and a.logId  = b.userId
    and a.userId = c.userId
    and a.logId = c.commentLogId
    and c.commentType = 0
    group by 1,2,3,4;
      

  2.   

    上面那个错了select distinct a.logTitle,a.logTitle,a.logTime,b.logTypeName,count(case when a.userId = c.userId and a.logId = c.commentLogId then 1 else 0 end case)
    from logs a,logType b,outer comments c
    where a.userId = 'xxxxxx'
    and a.userId = b.userId
    and a.logId  = b.userId
    and c.commentType = 0
    group by 1,2,3,4;
      

  3.   

    好像有错啊,在关键字 'case' 附近有语法错误。
      

  4.   

    select distinct a.logTitle,a.logTitle,a.logTime,b.logTypeName,count(case when a.userId = c.userId and a.logId = c.commentLogId then 1 else 0 end)
    from logs a,logType b,outer comments c
    where a.userId = 'xxxxxx'
    and a.userId = b.userId
    and a.logId  = b.userId
    and c.commentType = 0
    group by 1,2,3,4;
      

  5.   

    luxi0194(伊仪秋水) 
    你的好像也有问题,在关键字 'outer' 附近有语法错误。