I have a problem with mysql query.
I am writing SQL query to get ID and Timestamp from tablebusiness with different query
SELECT `ID`,`TimeStamp`,pow(-6-`Latitude`,2)+pow(106-`Longitude`,2)*cos(-6*0.017453292519943)*cos(`Latitude`*0.017453292519943) as DistanceSquare FROM `tablebusiness` WHERE Title LIKE '%sushi%' AND -6.8996746410157 < `Latitude` AND `Latitude` < -5.1003253589843 AND 105.10032535898 < `Longitude` AND `Longitude` < 106.89967464102 ORDER BY DistanceSquare LIMIT 2 ,20
Now this works fine in my case.
However I do not want DistanceSquare to be mentioned in the final result to save bandwidth. So I do
SELECT `ID`,`TimeStamp` FROM (SELECT `ID`,`TimeStamp`,pow(-6-`Latitude`,2)+pow(106-`Longitude`,2)*cos(-6*0.017453292519943)*cos(`Latitude`*0.017453292519943) as DistanceSquare FROM `开发者_StackOverflow中文版tablebusiness` WHERE Title LIKE '%sushi%' AND -6.8996746410157 < `Latitude` AND `Latitude` < -5.1003253589843 AND 105.10032535898 < `Longitude` AND `Longitude` < 106.89967464102 ORDER BY DistanceSquare LIMIT 2 ,20)
So basically I am picking up ID and TimeStamp from a table generated by the inner SELECT. It doesn't work. How should I format it?
I only want to get ID and TimeStamp from tablebusiness
can any one help me to solve this problem?
Not a big one but I just want to learn.
This is the PhP code I used to generate this query
$phiper180=pi()/180;
$formula="pow(".$_GET['lat']."-`Latitude`,2)+pow(".$_GET['long']."-`Longitude`,2)*cos(".$_GET['lat']."*".$phiper180.")*cos(`Latitude`*".$phiper180.")";
$query="SELECT `ID`,`TimeStamp`,$formula as DistanceSquare FROM `tablebusiness` WHERE Title LIKE '%".$_GET['keyword']."%' AND ". ($_GET['lat']-$distanceindegrees). " < `Latitude` AND `Latitude` < " . ($_GET['lat']+$distanceindegrees) . " AND " . ($_GET['long']-$distanceindegrees). " < `Longitude` AND `Longitude` < " . ($_GET['long']+$distanceindegrees)." ORDER BY DistanceSquare LIMIT ".($startFrom)." ,20";
$query="SELECT `ID`,`TimeStamp` FROM (".$query.")";
Why don't u just add (pow(-6-Latitude
,2)+pow(106-Longitude
,2)*cos(-6*0.017453292519943)*cos(Latitude
*0.017453292519943)) in order by clause no need to use sub query for that. Try following:
SELECT `ID`,`TimeStamp` FROM `tablebusiness` WHERE Title LIKE '%sushi%' AND -6.8996746410157 < `Latitude` AND `Latitude` < -5.1003253589843 AND 105.10032535898 < `Longitude` AND `Longitude` < 106.89967464102 ORDER BY (pow(-6-`Latitude`,2)+pow(106-`Longitude`,2)*cos(-6*0.017453292519943)*cos(`Latitude`*0.017453292519943)) LIMIT 2 ,20
精彩评论