数据库 mysql表1:文章表(article)id title content time
1  标题1   内容1    2011
2  标题2   内容2    2011 
3  标题3   内容3    2011表2:文章评论表(comment)
id article_id  time
1  1   评论1    2011
2  1   评论2    2011 
3  2   内容1    2011表是一对多关系要求如下;

按条件一次性查询文章表和文章评论表,找出文章和它的所有评论,并以文章分页,50条文章1页
问题1:用left join查的话 得到的记录分页不好分,确定不了每页50条文章。因为每条文章的评论数可能有0到n条,文章会有重复,去掉重复的就没50条了。问题2:如果先查文章,再根据文章查评论的话,速度会很慢。请问怎么做可以确定分页条数,速度又能过得去呢

解决方案 »

  1.   

    select content.*, comment.* from content, comment where content.id in (select id from content) and content.id = comment.id
    sql方面不好,你试一试这样的思路
      

  2.   

    select content.*, comment.* from content, comment where content.id in (select id from content limit X,50) and content.id = comment.id
      

  3.   

    用联合 unionselect id, title, content, time from article where 条件 limit 50
     union
     select article_id, '', content, time from comment
       where article_id in (select id from article where 条件 limit 50)
     order by id;
      

  4.   

    mysql不支持 子查询里有limit
      

  5.   


    #1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
      

  6.   

    那就做两句
    CREATE TEMPORARY TABLE temp select id, title, content, time from article where 条件 limit 50;select * from temp
     union
     select article_id, '', content, time from comment
      where article_id in (select id from temp)
     order by id;
      

  7.   

    -_-  请问这两句是一起执行的,还是分开执行啊
    一起执行 :Can't reopen table: 'temp'分开执行:#1146 - Table 'test.temp' doesn't exist
      

  8.   

    好无聊啊,mysql 怎么变成这样啦分三句
    CREATE TEMPORARY TABLE tmp1 select id, title, content, time from article where 条件 limit 50;CREATE TEMPORARY TABLE tmp2 SELECT id FROM tmp1;select * from tmp1
    union all
    select article_id, '', content, time from comment
      where article_id in (select id from temp)
     order by id;php:
    mysql_query('第一句');
    mysql_query('第二句');
    $rs = mysql_query('第三句');
      

  9.   


    在phpMyAdmin3.3.3来执行,同样是 一起执行 :Can't reopen table: 'tmp1'一句一句执行:#1146 - Table 'test.tmp1' doesn't exist
      

  10.   

    不用1,2句了,,select id, title, content,time from article where id in(1,2,3,4) limit 50 union
     select article_id, '', content,time from comment
      where article_id in (select t.id from (select * from article where id in(1,2,3,4) limit 50) as t)
     order by id;加多一层就好了