我有两个数据表分别是thread表 结构如下tid  ttitle tcontentcomment表 结构如下cid ccontent  tid(这里的是thread表的主键)我通过下面代码读出相关的数据public string GetNewsList()
    {        //省略
        SqlCommand cmd = new SqlCommand("sql语句", objConnection);//请问这里的SQL语句该怎么写?
        SqlDataReader dr = cmd.ExecuteReader();
        string strBody = null;
        while (dr.Read())
        {
            strBody += "标题"+dr["ttitle"]+"|内容"+dr["ttitle"]+"|回复"+  +"; //请问这里的回复该怎么写?回复的意思就是吧comment表中所有tid相同的分别统计一个和,用count(*)            }
        }
        //省略
    }我想实现的是,每个thread帖子下面都有该帖子相关的回复统计总数,请问这个该怎么写呢?谢谢

解决方案 »

  1.   

    sql语句select thread.tid, ttitle, tcontent, count(tid) as tidCount
    from thread inner join comment
    on thread.tid = comment.tid 
    group by thread.tid, ttitle, tcontent
      

  2.   

            while (dr.Read()) 
            { 
                strBody += "标题"+dr["ttitle"]+" ¦内容"+dr["ttitle"]+" ¦回复"+ dr["tidCount"] +";
            } 
      

  3.   

    来晚了,分数给抢了,只能总结一下public string GetNewsList() 
        {         //省略 
            String strSql=@"select thread.tid, ttitle, tcontent, count(tid) as tidCount 
                           from thread inner join comment 
                           on thread.tid = comment.tid 
                           group by thread.tid, ttitle, tcontent";
            SqlCommand cmd = new SqlCommand("sql语句", objConnection); 
            SqlDataReader dr = cmd.ExecuteReader(); 
            string strBody = null; 
            while (dr.Read()) 
            { 
                strBody += "标题"+dr["ttitle"]+" ¦内容"+dr["ttitle"]+" ¦回复"+ dr["tidCount"] +"; 
               } 
            } 
            //省略 
        } 
      

  4.   

    public string GetNewsList() 
        {         //省略 
            String strSql=@"select thread.tid, ttitle, tcontent, count(tid) as tidCount 
                          from thread inner join comment 
                          on thread.tid = comment.tid 
                          group by thread.tid, ttitle, tcontent"; 
            SqlCommand cmd = new SqlCommand("sql语句", objConnection); 
            SqlDataReader dr = cmd.ExecuteReader(); 
            string strBody = null; 
            while (dr.Read()) 
            { 
                strBody += "标题"+dr["ttitle"]+" ¦内容"+dr["ttitle"]+" ¦回复"+ dr["tidCount"] +"; 
              } 
            } 
            //省略 
        } 
      

  5.   

    public string GetNewsList()
        {        //省略
            String strSql=@"select thread.tid, ttitle, tcontent, count(tid) as tidCount
                          from thread inner join comment
                          on thread.tid = comment.tid
                          group by thread.tid, ttitle, tcontent";
            SqlCommand cmd = new SqlCommand("sql语句", objConnection);
            SqlDataReader dr = cmd.ExecuteReader();
            string strBody = null;
            while (dr.Read())
            {
                strBody += "标题"+dr["ttitle"]+" ¦内容"+dr["ttitle"]+" ¦回复"+ dr["tidCount"] +";
              }
            }
            //省略
        } 
    sql语句select thread.tid, ttitle, tcontent, count(tid) as tidCount
    from thread inner join comment
    on thread.tid = comment.tid
    group by thread.tid, ttitle, tcontent
            while (dr.Read())
            {
                strBody += "标题"+dr["ttitle"]+" ¦内容"+dr["ttitle"]+" ¦回复"+ dr["tidCount"] +";
            } 
      

  6.   

    select thread.tid, ttitle, tcontent, count(tid) as tidCount 
    from thread inner join comment 
    on thread.tid = comment.tid 
    group by thread.tid, ttitle, tcontent这个语句是能统计出来相对应的评论条数,但是遇到的问题是,其他没有评论的帖子就不出现了,显示出来的结果只是有评论的帖子
    而我需要的是,显示出所有帖子,并且在每个帖子下面有评论总数的统计,没有评论的,就显示为 0,谢谢