如果没有发布评论,您将如何显示评论计数,它将显示在表单视图中?

时间:2012-07-20 23:43:20

标签: asp.net mysql

我正在为学校建立一个博客。我想显示每个线程的评论计数。但是我对如何实现这个目标有点失落。任何帮助都会非常感谢你!

我有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`)
)

我的formview代码

<asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataSourceID="SqlDataSource1">      
                       <ItemTemplate>    
                           <div>
                           <asp:Label ID="lblcc" runat="server" Text="Comment Replys"></asp:Label>
                           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                           <asp:Label ID="lblT" runat="server" Text="Title"></asp:Label>
                           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                           <asp:Label ID="lblA" runat="server" Text="Author"></asp:Label>
                           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                           <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>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                   
                            <asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <asp:Label ID="lblAuthor" runat="server" Text='<%# Eval("Author") %>'></asp:Label>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <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>

我的目标是

在gridview中显示

表格格式:

评论计数|标题|用户名|创建日期

但是只有在帖子有评论时才会显示。如果线程有0条评论,我该如何显示0条评论?

1 个答案:

答案 0 :(得分:1)

使用外部联接而不是内部联接:

SELECT
    bm.MessageId,
    COUNT(bc.CommentId) AS comment_count,
    bm.AddedDate,
    bm.Author,
    bm.Title
FROM blog_message bm
LEFT JOIN blog_comments bc
ON bm.MessageId = bc.MessageId 
GROUP BY bm.MessageId
相关问题