473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Order results by derived value

Hi,

I'm looking for the most efficient way of displaying results from an SQL
query that must be ordered by a value that is derived following retrieval.

The user enters their location (postal code) and a search term (retailer
type). I can then query the DB for matching retailers and return their
geographic grid position. Based on this I can calculate the distance
between the user's location and the retailer. I'd like to display the
results ordered by the calculated distance. There could be hundreds of
matches so I'd also like to distribute the results over a number of pages
e.g. 1-10 on page 1, 11-20 on page 2, etc.

I have considered reading the data into a 2d array and sorting by distance
but distributing the results over many pages might be more difficult. I've
also considered creating a DB view but I'm not sure how to do this in
MySQL. I don't want to kill the server either!

This must have been solved many times by various people - I'd just like an
indication of which might be the best solution to avoid pursuing a deadend.

Tino.
Jul 17 '05 #1
1 1893

On 9-Apr-2004, Constantine Kakoushis <ti************@dsl.pipex.com> wrote:
I'm looking for the most efficient way of displaying results from an SQL
query that must be ordered by a value that is derived following retrieval.

The user enters their location (postal code) and a search term (retailer
type). I can then query the DB for matching retailers and return their
geographic grid position. Based on this I can calculate the distance
between the user's location and the retailer. I'd like to display the
results ordered by the calculated distance. There could be hundreds of
matches so I'd also like to distribute the results over a number of pages
e.g. 1-10 on page 1, 11-20 on page 2, etc.

I have considered reading the data into a 2d array and sorting by distance
but distributing the results over many pages might be more difficult. I've
also considered creating a DB view but I'm not sure how to do this in
MySQL. I don't want to kill the server either!

This must have been solved many times by various people - I'd just like an
indication of which might be the best solution to avoid pursuing a
deadend.


The simplest way to do this is to compute the distance with sql: "select
(distance computation) as distance,* from ... order by distance limit 1,10".
If you don't need the distance for anything but sorting you can "select *
from ... order by (distance computation) limit 1,10". You probably want to
ensure that the columns used by the distance computation are indexed.

--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
20
by: Xenophobe | last post by:
I have successfully converted the ASP code included in the following article to PHP: http://www.4guysfromrolla.com/webtech/040100-1.shtml As described the high and low latitudes and longitudes...
39
by: Nicolas Fleury | last post by:
In the following example: class MyMetaclass(type): pass class MyBaseType(object): __metaclass__ = MyMetaclass class MyType(MyBaseType): x = 4 y = 5 z = 6 Is there any way to modify...
18
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
13
by: z. f. | last post by:
Hi, i have a class that is derived from System.Web.UI.Page, and this is the class i use in my application as PageBase. all other page classes are deriverd from my PageBase instead of the...
5
by: chadsmith76 | last post by:
hello, I have listings with coordinates, i would like to do ORDER BY and display listings with coords first. If they don't have coords the value is blank and the coords go both positive and...
1
by: Brit | last post by:
I have an ASP file that retrieves names from an Access database for 4 different categories of membership, which the visitor to the page selects (corporate, institutional, regular, or student). The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.