Php 在MySQL中从子查询抓取查询

Php 在MySQL中从子查询抓取查询,php,mysql,sql,database,select,Php,Mysql,Sql,Database,Select,我对mysql查询有问题 我正在编写SQL查询,以便使用不同的查询从tablebusiness获取ID和时间戳 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%'

我对mysql查询有问题

我正在编写SQL查询,以便使用不同的查询从tablebusiness获取ID和时间戳

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
现在这在我的情况下很好用

然而,我不想在最终结果中提到DistanceSquare来节省带宽。我也是

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 `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)
基本上,我从一个由内部SELECT生成的表中提取ID和时间戳。它不起作用。我应该如何格式化它

我只想从tablebusiness获取ID和时间戳

有人能帮我解决这个问题吗

不是很大,但我只是想学习

这是我用来生成这个查询的PhP代码

$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.")";

为什么不在ORDERBY子句中添加pow-6纬度,2+pow106经度,2*cos-6*0.017453292519943*COSLATION*0.017453292519943,无需使用子查询。请尝试以下操作:

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

啊,很好用。效果很好。实际上,我一直在考虑使用存储过程来实现这一点,但似乎有几种方法可以实现。