MySQL距离查询及在PHP中的使用和

MySQL距离查询及在PHP中的使用和,php,mysql,distance,Php,Mysql,Distance,我需要MySQL查询专家的帮助。我在MySQL数据库中有纬度和经度。我可以从查询中获得最近的位置,但当我尝试在查询中添加“AND”时,我得到了一个错误 我做了很多研究,但没有找到答案 $lat_database是用户请求的纬度 $lonu数据库是用户请求的经度 latitude-MySQL数据库中的latitude列名称 经度-MySQL数据库中的经度列名 选择*, ((ACOS(SIN($lat_database*PI()/180) *SIN(纬度*PI()/180) +COS($lat

我需要MySQL查询专家的帮助。我在MySQL数据库中有纬度和经度。我可以从查询中获得最近的位置,但当我尝试在查询中添加“AND”时,我得到了一个错误

我做了很多研究,但没有找到答案

  • $lat_database
    是用户请求的纬度
  • $lonu数据库
    是用户请求的经度
  • latitude
    -MySQL数据库中的latitude列名称
  • 经度
    -MySQL数据库中的经度列名

选择*,
((ACOS(SIN($lat_database*PI()/180)
*SIN(纬度*PI()/180)
+COS($lat_数据库*PI()/180)
*COS(纬度*PI()/180)
*COS($lonu数据库-经度)*PI()/180)
)*180/PI()
) * 60 * 1.1515
)作为距离

距离使用连接和使用后

$sql = "SELECT *, ((ACOS(SIN($lat_database * PI() / 180) * SIN(latitude * PI() / 180) + COS($lat_database * PI() / 180) * COS(latitude * PI() / 180) * 
COS(($lon_database - longitude) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) 
as distance FROM locations
join items on items.id = t1.item_id
where item LIKE ('%" . $item . "%')
 HAVING distance <= 20 
 ORDER BY distance ASC $limit";
$sql=“SELECT*,((ACOS(SIN($lat_数据库*PI()/180)*SIN(纬度*PI()/180)+COS($lat_数据库*PI()/180)*COS(纬度*PI()/180)*
COS($lonu数据库-经度)*PI()/180))*180/PI())*60*1.1515)
与位置的距离
连接items.id=t1.item\u id上的项目
其中项目类似(“%”“$item.”%

如果您的查询有两个
FROM
子句,这在SQL中是不允许的。您想做什么?示例数据和预期结果会有所帮助。啊,对不起!我在处理查询,所以有两个FROM。以下是查询:$SQL=“SELECT*,((ACOS(SIN($lat_database*PI()/180)*SIN latitude*PI()/180)+COS($lat_database*PI()/180)*COS(纬度*PI()/180)*COS($lon_database-经度)*PI()/180))*180/PI())*60*1.1515)作为距离作为距离作为距离我正在尝试使用用户给定的位置查找20英里内的所有项目。HAVING子句位于from之后,而不是之前。非常感谢,Hossein!我如何计算所有记录(选择上面的查询返回的计数(唯一\u id))?将sql代码注入此代码
从($sql)选择计数(q1.unique\u id)作为q1
$sql = "SELECT *, ((ACOS(SIN($lat_database * PI() / 180) * SIN(latitude * PI() / 180) + COS($lat_database * PI() / 180) * COS(latitude * PI() / 180) * 
COS(($lon_database - longitude) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) 
as distance FROM locations
join items on items.id = t1.item_id
where item LIKE ('%" . $item . "%')
 HAVING distance <= 20 
 ORDER BY distance ASC $limit";