简体   繁体   中英

MySql Order query by value in where clause

Here is my query

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)

I would like the resultset to be sorted in order of values i am using in where clause in between condition. so my result set would be in order

19.11817534
19.11810438
19.11783068
19.11744546
19.11697916
19.11598571
19.11563091

is that possible?

Thanks, Rizwan

I don't think you are going to have a way to do that other than to break that down into multiple queries performed in your order of choice.

The problem is you don't have any actual field to sort on or way to associate with records in the results set were associated with with condition in the WHERE clause (they could in fact match multiple conditions).

As an alternative, you might be able to create a temp memory table of your filter data like:

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

An then query across a join of the tables:

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

Obviously, make sure you have indexes on all min/max lat/long fields in a_table .

I am not sure how you would want to handle duplicates here (may need to do SELECT DISTINCT on whatever the primary key is for a_table )

you can use an ORDER BY clause to set the ordering. If your column was called latitude you would use

ORDER BY latitude

If you're generating your WHERE clause from a loop you could also use a CASE statement

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
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

Will this work for you?

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

I'm not understanding how you dont have access to the table? but this is my solution:

Use PHP or whatever language you want. The easiest way to sort your result set will be to store the results in an array and order them.

$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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM