MySql按where子句中的值查询

时间:2013-04-03 21:08:47

标签: mysql sql-order-by

这是我的查询

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

可能吗?

谢谢, RIZWAN

5 个答案:

答案 0 :(得分:1)

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

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

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

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

然后查询表的连接:

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

答案 1 :(得分:0)

您可以使用ORDER BY子句设置排序。如果您的列名为latitude,则使用

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

答案 2 :(得分: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

答案 3 :(得分: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

答案 4 :(得分: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;
}