有三个表(新闻类型表newsType,新闻表news,评论表comments)
新闻表里有新闻类型的主键ID:newsTypeId,评论表里有新闻表的主键ID:newsId,怎样查询一条新闻是什么类型,有多少条评论,以及新闻标题,新闻ID。
新闻表里有新闻类型的主键ID:newsTypeId,评论表里有新闻表的主键ID:newsId,怎样查询一条新闻是什么类型,有多少条评论,以及新闻标题,新闻ID。
from news a join newstype b on a.newsTypesId = b.newsTypesId
join comments c a.newsId = c.newsId
group by b.newtype, a.标题, a.新闻ID
newstype.newstypeid,
COUNT(*) AS counter
FROM news INNER JOIN newstype
ON news.newsid = nestype.newsid
INNER JOIN comments
ON news.newsid = comments.newsid
GROUP BY news.newsid,newstype.newstypeid
(
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, --评论时间
)表的结构是这样的