How to sort complicated PHP queries.

Source: Internet
Author: User
How to sort complicated PHP queries. $ Latitude =$ _ GET ['Y'];
$ Longpolling = $ _ GET ['x'];
$ Distance = 500;
$ SQL = "select * from ". $ GLOBALS ['ecs']-> table ('weixin _ map '). "where sqrt ((((". $ longpolling. "-longpolling) * PI () * 12656 * cos (((". $ latitude. "+ Latitude)/2) * PI ()/180)/180 )*((". $ longpolling. "-longpolling) * PI () * 12656 * cos (((". $ latitude. "+ Latitude)/2) * PI ()/180)/180) + (((". $ latitude. "-Latitude) * PI () * 12656/180 )*((". $ latitude. "-Latitude) * PI () * 12656/180)/2 <". $ distance;

I found the merchant information 500 km ago, but I cannot sort it by distance.
Check the specific method.


Reply to discussion (solution)

How to Use array to calculate the distance of all information and then sort it again.

My idea is to create an array, calculate each piece of data, calculate the distance, and then sort the data.
Who can help write it out...

Select *, sqrt ((((". $ longpolling. "-longpolling) * PI () * 12656 * cos (((". $ latitude. "+ Latitude)/2) * PI ()/180)/180 )*((". $ longpolling. "-longpolling) * PI () * 12656 * cos (((". $ latitude. "+ Latitude)/2) * PI ()/180)/180) + (((". $ latitude. "-Latitude) * PI () * 12656/180 )*((". $ latitude. "-Latitude) * PI () * 12656/180)/2 as dis
Order by dis having dis in the from table <= 500

Select *, sqrt ((((". $ longpolling. "-longpolling) * PI () * 12656 * cos (((". $ latitude. "+ Latitude)/2) * PI ()/180)/180 )*((". $ longpolling. "-longpolling) * PI () * 12656 * cos (((". $ latitude. "+ Latitude)/2) * PI ()/180)/180) + (((". $ latitude. "-Latitude) * PI () * 12656/180 )*((". $ latitude. "-Latitude) * PI () * 12656/180)/2 as dis
Order by dis having dis in the from table <= 500



Error occurred, prompting having dis errno

Select *, sqrt ((((". $ longpolling. "-longpolling) * PI () * 12656 * cos (((". $ latitude. "+ Latitude)/2) * PI ()/180)/180 )*((". $ longpolling. "-longpolling) * PI () * 12656 * cos (((". $ latitude. "+ Latitude)/2) * PI ()/180)/180) + (((". $ latitude. "-Latitude) * PI () * 12656/180 )*((". $ latitude. "-Latitude) * PI () * 12656/180)/2 as dis
Order by dis having dis in the from table <= 500



$ SQL = "select *, sqrt ((((". $ longpolling. "-longpolling) * PI () * 12656 * cos (((". $ latitude. "+ Latitude)/2) * PI ()/180)/180 )*((". $ longpolling. "-longpolling) * PI () * 12656 * cos (((". $ latitude. "+ Latitude)/2) * PI ()/180)/180) + (((". $ latitude. "-Latitude) * PI () * 12656/180 )*((". $ latitude. "-Latitude) * PI () * 12656/180)/2 as dis
From ". $ GLOBALS ['ecs']-> table ('weixin _ map')." group by dis having dis <". $ distance;

This can be sorted, but the opposite is true ..

Select *, sqrt ((((". $ longpolling. "-longpolling) * PI () * 12656 * cos (((". $ latitude. "+ Latitude)/2) * PI ()/180)/180 )*((". $ longpolling. "-longpolling) * PI () * 12656 * cos (((". $ latitude. "+ Latitude)/2) * PI ()/180)/180) + (((". $ latitude. "-Latitude) * PI () * 12656/180 )*((". $ latitude. "-Latitude) * PI () * 12656/180)/2 as dis
From ". $ GLOBALS ['ecs']-> table ('weixin _ map')." group by dis desc having dis <". $ distance;

Solution.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.