一共三个表
news表
n_id
n_title
n_zz
n_time
n_tj
n_content
isdel
class_idclass表
class_id
class_icocomments
c_id
n_id
c_commentselect * from (SELECT top 10 * FROM (select top " + countcurrentpage + " c.class_id as class_id, n_id,n_title,n_zz,n_time,n_tj,class_ico,n_content  from news n,class c where n.class_id=c.class_id and n.isdel=0 order by n_id desc) t order by n_id asc) t2 order by n_id desc上面的SQL我加入了分页的东西,已经完成了我想逐条列出数据表news的信息,并显示出她所在分类,但现在我现在想增加一个对每条news的数据,加上对应的评论的条数,这就是博客中的,在每篇文章下面列出评论的条数,请问这个SQL语句该怎么改呢?

解决方案 »

  1.   


    select a.n_id,a.n_content,count(b.c_id) from news a
    left join comments b
    on a.n_id=b.n_id
    group by a.n_id,a.n_content
      

  2.   

    select * 
    from 
    (SELECT top 10 * 
     FROM (
    select top " + countcurrentpage + " 
    c.class_id as class_id
    , n_id
    ,n_title
    ,n_zz
    ,n_time
    ,n_tj
    ,class_ico,n_content
    ,select count(*) from comments where n_id = n.n_id
    from news n,class c
    where n.class_id=c.class_id and n.isdel=0 order by n_id desc
    ) t 
     order by n_id asc) t2 
    order by n_id desc 
      

  3.   

    select count(*) from comments where n_id = n.n_id
    这句价格括号,然后在后面加个东西就可以了,谢谢