以eid,cid相同为一类,取出某个分类的前几条,怎么写?
eid  cid   value
1     1       1
1     1       2
1     1       3
1     2       1
1     2       2
1     2       3
1     3       1比如要取前2条
1  1   1
1  1   2
1  2   1
1  2   2
1  3   1

解决方案 »

  1.   

    select *
    from tb t
    where
    (select count(1) from tb where eid=t.eid and cid=t.cid and value<=t.value)<=2
      

  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>
      

  3.   

    可以参考一下这个贴子。http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
    [征集]分组取最大N条记录方法征集
      

  4.   

    如果最后value改成时间的话,求最新时间的前2条
    select *
    from t_xinlan1022 a
    where 2>(select count(*) from t_xinlan1022 where eid=a.eid and cid=a.cid and time<a.time);求出来的不对哦,求高手指点
      

  5.   

    你的 time 是什么字段,举个不对的例子贴出来分析一下。
      

  6.   

    date 类型应该没有问题啊。建议举个例子说明一下不正确的那个记录。
      

  7.   

    就是几个时间相同的时候会出现问题,不过后来找了个int类型的作比较就对了