取某个分类的前几个条(mysql怎么写) 以eid,cid相同为一类,取出某个分类的前几条,怎么写?eid cid value1 1 11 1 21 1 31 2 11 2 21 2 31 3 1比如要取前2条1 1 11 1 21 2 11 2 21 3 1 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select *from tb twhere(select count(1) from tb where eid=t.eid and cid=t.cid and value<=t.value)<=2 mysql> select * from t_xinlan1022;+------+------+-------+| eid | cid | value |+------+------+-------+| 1 | 1 | 1 || 1 | 1 | 2 || 1 | 1 | 3 || 1 | 2 | 1 || 1 | 2 | 2 || 1 | 2 | 3 || 1 | 3 | 1 |+------+------+-------+7 rows in set (0.03 sec)mysql>mysql> select * -> from t_xinlan1022 a -> where 2>(select count(*) from t_xinlan1022 where eid=a.eid and cid=a.cidand `value`<a.value);+------+------+-------+| eid | cid | value |+------+------+-------+| 1 | 1 | 1 || 1 | 1 | 2 || 1 | 2 | 1 || 1 | 2 | 2 || 1 | 3 | 1 |+------+------+-------+5 rows in set (0.09 sec)mysql> 可以参考一下这个贴子。http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html[征集]分组取最大N条记录方法征集 如果最后value改成时间的话,求最新时间的前2条select *from t_xinlan1022 awhere 2>(select count(*) from t_xinlan1022 where eid=a.eid and cid=a.cid and time<a.time);求出来的不对哦,求高手指点 你的 time 是什么字段,举个不对的例子贴出来分析一下。 date 类型应该没有问题啊。建议举个例子说明一下不正确的那个记录。 就是几个时间相同的时候会出现问题,不过后来找了个int类型的作比较就对了 mysql创建外键的问题,看看我的语句有什么错啊,在线等 Commands out of sync; you can't run this command now? sql查询语句——请教各位大牛 (弄了一天了,我是无力了) 菜鸟MySql安装问题 语句不走索引(复合字段索引) 两条SQL语句的优化,急!急!急!请大家多多指点 mysql 获取空列的列名 急求一sql,高手请进 请问在delphi中使用mysql插入图片与在别的数据库中的操作有什么不同嘛? 记录分组后如何按时间排序? 数据库高手请进,关于表的设计和查询的问题 请教一个关于学生选课的问题
from tb t
where
(select count(1) from tb where eid=t.eid and cid=t.cid and value<=t.value)<=2
+------+------+-------+
| eid | cid | value |
+------+------+-------+
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 1 | 3 |
| 1 | 2 | 1 |
| 1 | 2 | 2 |
| 1 | 2 | 3 |
| 1 | 3 | 1 |
+------+------+-------+
7 rows in set (0.03 sec)mysql>
mysql> select *
-> from t_xinlan1022 a
-> where 2>(select count(*) from t_xinlan1022 where eid=a.eid and cid=a.cidand `value`<a.value);
+------+------+-------+
| eid | cid | value |
+------+------+-------+
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 2 | 1 |
| 1 | 2 | 2 |
| 1 | 3 | 1 |
+------+------+-------+
5 rows in set (0.09 sec)mysql>
[征集]分组取最大N条记录方法征集
select *
from t_xinlan1022 a
where 2>(select count(*) from t_xinlan1022 where eid=a.eid and cid=a.cid and time<a.time);求出来的不对哦,求高手指点