我正在为学校建立一个博客。我想显示每个线程的评论数。但是,我对如何实现这一目标有点迷茫。任何帮助都会非常感谢您!
我有 2 张桌子
CREATE TABLE `blog_message` (
`MessageID` int(30) NOT NULL AUTO_INCREMENT,
`Username` varchar(45) NOT NULL,
`Message` text,
`AddedDate` datetime DEFAULT NULL,
`Title` varchar(45) DEFAULT NULL,
PRIMARY KEY (`MessageID`)
)
CREATE TABLE `blog_comments` (
`CommentID` int(30) NOT NULL AUTO_INCREMENT,
`MessageID` int(30) DEFAULT NULL,
`Author` varchar(45) DEFAULT NULL,
`CommentMessage` text,
`AddedDate` datetime DEFAULT NULL,
PRIMARY KEY (`CommentID`),
KEY `blog_comments_ibfk_1` (`MessageID`),
CONSTRAINT `blog_comments_ibfk_1` FOREIGN KEY (`MessageID`) REFERENCES `blog_message` (`MessageID`)
)
我的表单视图代码
<asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataSourceID="SqlDataSource1">
<ItemTemplate>
<div>
<asp:Label ID="lblcc" runat="server" Text="Comment Replys"></asp:Label>
<asp:Label ID="lblT" runat="server" Text="Title"></asp:Label>
<asp:Label ID="lblA" runat="server" Text="Author"></asp:Label>
<asp:Label ID="lbldoc" runat="server" Text="Date of Creation"></asp:Label>
</div>
<hr />
<asp:LinkButton ID="LinkButton1" runat="server" Font-Underline="false" PostBackUrl='<%# Eval("MessageID", "BlogComments.aspx?MessageID={0}") %>' >
<asp:Label ID="lblCommentCounts" runat="server" Text='<%# Eval("comment_count") %>'></asp:Label>
<asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
<asp:Label ID="lblAuthor" runat="server" Text='<%# Eval("Author") %>'></asp:Label>
<asp:Label ID="lbDateOfCreation" runat="server" Text='<%# Eval("AddedDate") %>'></asp:Label>
</asp:LinkButton>
<hr />
</ItemTemplate>
</asp:FormView>
MySQL 代码
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT bm.MessageId, count(bc.CommentId) as comment_count, bm.AddedDate, bm.Author, bm.Title
FROM blog_comments bc, blog_message bm
WHERE bm.MessageId = bc.MessageId
GROUP BY bm.MessageId"></asp:SqlDataSource>
我的目标是
在网格视图中显示
表格格式:
评论数 | 标题 | 用户名 | 创建日期
但是,它只会在线程有评论时显示。如果线程有 0 条评论,我如何显示 0 条评论?