我正在尝试将两个quires合二为一。第一个问题是让位于3KM位置的用户和第二个查询是对他们工作的位置进行排序。 所以我想加入两个quires,这样我就可以让那些在3KM左右的特定公司工作的人。 工作是一个具有各种公司名称的专栏。
http://victorybro.esy.es/LocateMe/locateNearme.php?work[]=HCL&work[]=DELL
此链接指出:“查询失败!”
这是我的Php代码:
<?php
$con = mysqli_connect("mysql.hostinger.in", "u408122646_chat", "123", "u408122646_chat");
$work = $_GET['work'];
$locate_me = array();
foreach ($_REQUEST['work'] as $key => $val) {
$locate_me[$key] = filter_var($val, FILTER_SANITIZE_STRING);
}
$works = "'" . implode("','", $locate_me) . "'";
$sql="SELECT id,latitude,londitude, ( 6371 * acos( cos( radians(12.95665699) ) * cos( radians( latitude ) ) * cos( radians( londitude ) - radians(80.23708535) ) + sin( radians(12.95665699) ) * sin( radians( latitude ) ) ) ) AS distance FROM locate_me HAVING distance < 1 ORDER BY distance LIMIT 0 , 20
UNION
SELECT * FROM locate_me WHERE work IN ({$works})";
$res=mysqli_query($con,$sql);
if(!$res)
{
die("Query Failed!");
} $response["success"] = true;
$response= array();
while($row = $res->fetch_array(MYSQL_ASSOC)) {
$response[] = $row;}
echo json_encode(array("response"=>$response));
mysqli_free_result($res);
?>
答案 0 :(得分:0)
如果我理解你需要什么,那么SQL可以写成:
$myLocLat = 12.95665699;
$myLocLon = 80.23708535;
$sql = <<<SQL
SELECT
id,
latitude,
londitude,
(6371 * acos(cos(radians($myLocLat)) * cos(radians(latitude)) * cos(radians(londitude) - radians($myLocLon)) + sin(radians($myLocLat)) * sin(radians(latitude)))) AS distance
FROM locate_me
WHERE distance < 1
AND work IN ($works)
ORDER BY distance LIMIT 0 , 20
SQL;
请注意,在MySQL 5.7.6+中,您可以通过以下方式获得两点之间的距离:
ST_Distance(Point(lon1,lat1),Point(lon2,lat2))
如果你不想处理确定距离的公式,可以考虑一些事情。