日期:2014-05-16  浏览次数:20756 次

MYSQL分类取数据问题
我想每个类别只取-条数据,下面这语句执行好慢,有时好几十秒,有时又很快,是什么原因?
select t.ename,t.ntype,a.addtime,a.id,a.title from learn a inner join ntype t on t.ntypeid=a.ntypeid where a.flag=1 and 1 > (select count(id) from learn b where b.ntypeid= a.ntypeid and b.id> a.id)order by a.id desc limit 8


MYSQL里有没有类似MSSQL里的CROSS APPLY?

------解决方案--------------------
探讨
我想每个类别只取-条数据,下面这语句执行好慢,有时好几十秒,有时又很快,是什么原因?
select t.ename,t.ntype,a.addtime,a.id,a.title from learn a inner join ntype t on t.ntypeid=a.ntypeid where a.flag=1 and 1 > (select count(id) from learn b w……

------解决方案--------------------
假设ID唯一
select * from learn a where not exists(select 1 from learn where a.ntypeid=ntypeid and a.id<id)
------解决方案--------------------
参考下贴中的多种方法

http://blog.csdn.net/acmain_chm/article/details/4126306
[征集]分组取最大N条记录方法征集,及散分....