0

这是我的查询

select * from a_table where  
(19.1181753366684 between minlatitude and maxlatitude ) and (72.8504168987274 between minlongitude and maxlongitude) or
 (19.1181043770386 between minlatitude and maxlatitude ) and (72.8506743907928 between minlongitude and maxlongitude) or
 (19.1178306753238 between minlatitude and maxlatitude ) and (72.8506422042847 between minlongitude and maxlongitude) or
(19.1174454647353 between minlatitude and maxlatitude ) and (72.8505992889404 between minlongitude and maxlongitude) or
(19.1169791559797 between minlatitude and maxlatitude ) and (72.8505349159241 between minlongitude and maxlongitude) or
(19.1159857112009 between minlatitude and maxlatitude ) and (72.8504061698914 between minlongitude and maxlongitude) or
(19.1156309080473 between minlatitude and maxlatitude ) and (72.8503739833832 between minlongitude and maxlongitude) or
(19.1152862413976 between minlatitude and maxlatitude ) and (72.8502130508423 between minlongitude and maxlongitude)

我希望结果集按照我在条件之间的 where 子句中使用的值的顺序进行排序。所以我的结果集是有序的

19.11817534
19.11810438
19.11783068
19.11744546
19.11697916
19.11598571
19.11563091

那可能吗?

谢谢, 里兹万

4

5 回答 5

1

我认为除了将其分解为按您选择的顺序执行的多个查询之外,您没有其他方法可以做到这一点。

问题是您没有要排序的任何实际字段或与结果集中的记录相关联的方法,这些记录与 WHERE 子句中的条件相关联(它们实际上可以匹配多个条件)。

作为替代方案,您可以创建过滤器数据的临时内存表,例如:

id   latitude          longitude
1    19.1181753366684  72.8504168987274
2    19.1181043770386  72.8506743907928
...

跨表连接的 then 查询:

SELECT a_table.*, temp_table.id
FROM
a_table INNER JOIN temp_table
  ON temp_table.latitude BETWEEN a_table.minlatitude AND a_table.maxlatitude
  AND temp_table.longitude BETWEEN a_table.minlongitude AND a_table.maxlongitude
ORDER BY temp_table.id ASC

显然,请确保您在a_table.

我不确定您想如何在这里处理重复项(可能需要对SELECT DISTINCT主键的用途进行处理a_table

于 2013-04-03T21:14:02.947 回答
0

您可以使用ORDER BY子句来设置排序。如果您的列被称为纬度,您将使用

ORDER BY latitude

如果您从循环中生成 WHERE 子句,您还可以使用 CASE 语句

ORDER BY CASE
  loop
    WHEN latitude = 19.11817534 THEN 1 --change the two values dynamically. 1 would be a counter
  end loop
ELSE 999 END
于 2013-04-03T21:10:31.923 回答
0
SELECT  *
FROM    (
        SELECT  19.1181753366684 AS lat, 72.8504168987274 AS lon, 1 AS no
        UNION ALL
        SELECT  19.1181043770386 AS lat, 72.8506743907928 AS lon, 2 AS no
        UNION ALL
        ...
        ) p
JOIN    a_table a
ON      p.lat BETWEEN a.minlatitude AND a.maxlatitude
        AND p.lon BETWEEN a.minlongitude AND a.maxlongitude
ORDER BY
        no
于 2013-04-03T21:14:40.563 回答
0

这对你有用吗?

select * from a_table where  
(19.1181753366684 between minlatitude and maxlatitude ) and (72.8504168987274 between minlongitude and maxlongitude) or
(19.1181043770386 between minlatitude and maxlatitude ) and (72.8506743907928 between minlongitude and maxlongitude) or
 (19.1178306753238 between minlatitude and maxlatitude ) and (72.8506422042847 between minlongitude and maxlongitude) or
(19.1174454647353 between minlatitude and maxlatitude ) and (72.8505992889404 between minlongitude and maxlongitude) or
(19.1169791559797 between minlatitude and maxlatitude ) and (72.8505349159241 between minlongitude and maxlongitude) or
(19.1159857112009 between minlatitude and maxlatitude ) and (72.8504061698914 between minlongitude and maxlongitude) or
(19.1156309080473 between minlatitude and maxlatitude ) and (72.8503739833832 between minlongitude and maxlongitude) or
(19.1152862413976 between minlatitude and maxlatitude ) and (72.8502130508423 between minlongitude and maxlongitude)
ORDER BY maxlatitude, minlatitude DESC
于 2013-04-03T21:21:32.560 回答
0

我不明白您如何无法访问该表?但这是我的解决方案:

使用 PHP 或任何你想要的语言。对结果集进行排序的最简单方法是将结果存储在数组中并对其进行排序。

$result = execute(select * from a_table where (19.1181753366684 between minlatitude and maxlatitude ) and ...);

asort($result);

foreach($result AS $row){
    //do what you want in here
    echo $row;
}
于 2013-04-03T21:44:52.600 回答