大家好,网站有一个帖子表和回复表
原来帖子表没有设置最后回复字段和回复数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.*,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
解决方案 »
- mysql插入数据为什么很慢?
- 数据库 两个表中各取一部分组成一个新表
- 使用游标出现 No data - zero rows fetched, selected, or processed
- 特殊的字符串拆分
- 请教类\子类的表如何设置最好?
- 项目紧急求救啊,mssql存储过程迁移到mysql。
- 如何取得select结果数据集的前10条记录。postgresql
- [200分]在程序操作mysql数据库时,到底mysql的password函数该怎样用!
- 菜鸟送分!怎么才能读取.frm的文件
- 密码正确不能登录
- 逗号相隔字段值查询的最优方法是什么呢?
- mysql里show processlist里的一个表查询status显示为locked 访问设计这个表的网页就出现问题
select a.*, count(*) replies from ask_text a, ask_post p where a.id=p.parentid group by p.parentid order by replies desc
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式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)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
非常感谢,运行了下是我想要的结果.有一点不是很明白,
就是单纯的group by只是分组,但多条记录还是会重复出来
只有加入统计函数比如max,min,avg,count才能得到单条记录?