求一Sql语句,在线等了。 查询某表x字段最小和y字段最小的各两条记录,即4条记录,同时取得各记录其他字段值。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 示例:SELECT * FROM abf a1 WHERE 2>=(SELECT COUNT(1) FROM abf WHERE a1.x<=x )OR2>=(SELECT COUNT(1) FROM abf WHERE a1.y>=y ) 数据表的内容是一个地点数据表,有X坐标,Y坐标,现在想取X坐标最小的两条,Y坐标最小的两条记录,共4条。 select * from (select top 2 * from T order by T1) A Union Allselect * from (select top 2 * from T order by T2) B 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条。这个说明白了吗? 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) 参考下贴中的多种方法http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html[征集]分组取最大N条记录方法征集,及散分.... (不要高估你的汉语表达能力或者我的汉语理解能力) 建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。 参考一下这个贴子的提问方式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) 这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。 调用MySql带有返回参数的存储过程,获取返回参数值? asp与数据库Mysql连接问题 求一句Mysql语句 mysql里where条件中包含or应该怎么建立有效的索引。 PostgreSQL数据类型问题 下载了个mysql5的非安装版,不知怎么运行起来? 能不能用ADO从Windows连接Linux上的Mysql数据库? "大"=="呆"? about Mysql query 求根据两列的去重的SQL语句 mysql非安装版启动时是报1067错误 Mysql的给一个有数据的表添加字段,添加不进去 问一条sql语句的写法
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 )
Union All
select * from (select top 2 * from T order by T2) B
+----------+------------+-------+-------+
| 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)
[征集]分组取最大N条记录方法征集,及散分....
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式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)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。