大家好,网站有一个帖子表和回复表
原来帖子表没有设置最后回复字段和回复数1:按最新回复排序:之前用
select a.*,p.* from ask_text a,ask_post p where a.id=p.parentid group by p.parentid order by p.postdate desc
显示结果有重复?谁可以帮解释下为什么groupby后还会重复吗?后来使用这个写法就没问题
select a.*,p.* from ask_text a,ask_post p where a.id=p.parentid and p.postdate=(select postdate from ask_post where ask_post.parentid=a.id order by postdate desc limit 0,1) order by p.postdate desc";
2 如果要按回复最多排序 sql语句要怎么写?
帖子表 ask_text -> id,title,createdate
回复吧 ask_post -> id,parentid,postdate
ask_text.id=ask_post.parentid

解决方案 »

  1.   

    select a.*, max(p.postdate) lastdate from ask_text a, ask_post p where a.id=p.parentid group by p.parentid order by lastdate desc
    select a.*, count(*) replies from ask_text a, ask_post p where a.id=p.parentid group by p.parentid order by replies desc
      

  2.   

     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  3.   

    #shine333
    非常感谢,运行了下是我想要的结果.有一点不是很明白,
    就是单纯的group by只是分组,但多条记录还是会重复出来
    只有加入统计函数比如max,min,avg,count才能得到单条记录?