查询某表x字段最小和y字段最小的各两条记录,即4条记录,同时取得各记录其他字段值。

解决方案 »

  1.   

    示例:
    SELECT * FROM abf a1 WHERE 2>=(SELECT COUNT(1) FROM abf WHERE a1.x<=x  )OR
    2>=(SELECT COUNT(1) FROM abf WHERE a1.y>=y  )
      

  2.   

    数据表的内容是一个地点数据表,有X坐标,Y坐标,现在想取X坐标最小的两条,Y坐标最小的两条记录,共4条。
      

  3.   

    select * from (select top 2 * from T order by T1) A 
    Union All
    select * from (select top 2 * from T order by T2) B
      

  4.   

    point_id,point_name,map_x,map_y 数据表结构,map_x最小的两条,比如map_x有1,2,3,4..., 取出值为1,2的各一条或值为1的两条,map_y同理.但要求同时取出符合map_x,符合map_y的各2条。这个说明白了吗?
      

  5.   

    mysql> select * from  test;
    +----------+------------+-------+-------+
    | point_id | point_name | map_x | map_y |
    +----------+------------+-------+-------+
    |        1 | test1      |     1 |     1 |
    |        2 | test2      |     2 |     3 |
    |        3 | test3      |     3 |     2 |
    |        4 | test4      |     1 |     5 |
    |        5 | test5      |     3 |     1 |
    +----------+------------+-------+-------+
    5 rows in set (0.00 sec)mysql> (select * from test order by map_x limit 2) union all (select * from test order by map_y limit 2);
    +----------+------------+-------+-------+
    | point_id | point_name | map_x | map_y |
    +----------+------------+-------+-------+
    |        1 | test1      |     1 |     1 |
    |        4 | test4      |     1 |     5 |
    |        1 | test1      |     1 |     1 |
    |        5 | test5      |     3 |     1 |
    +----------+------------+-------+-------+
    4 rows in set (0.00 sec)
      

  6.   

    参考下贴中的多种方法http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
    [征集]分组取最大N条记录方法征集,及散分....
      

  7.   

     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式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)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。