php - 从 MySQL 中的另一个查询调用查询并仅获取 1 个 JSON

标签 php mysql sql json json.net

我想从另一个表中调用并选择对另一个查询的查询,并仅发送一个 JSON。我有 2 个表(towing_listtowing_info)

json获取方法

[{"towing_id":"51","towing_username":"tow","towing_latitude":"3.7310769",
"towing_longitude":"103.1240930","distance":"0"},
    {"towing_id":"56","towing_username":"tow1","towing_latitude":"3.7311311",
"towing_longitude":"103.1239854","distance":"0.013374089073083037"}]

我想使用“towing_username”并从另一个表中调用他们的详细信息,希望是他们的“towing_fullname”和“towing_contactnumber”,这样它将得到下面的 json 结果:

[{"towing_id":"51","towing_username":"tow","towing_fullname":"tow_name",
"towing_contactnumber":"0123456789","towing_latitude":"3.7310769",
"towing_longitude":"103.1240930","distance":"0"},
{"towing_id":"56","towing_username":"tow1","towing_fullname":"tow1_name",
"towing_contactnumber":"01518191904","towing_latitude":"3.7311311",
"towing_longitude":"103.1239854","distance":"0.013374089073083037"}]

我的 table

towing_list : (towing_id,towing_username,towing_latitude,towing_longitude)
towing_info : (towing_id,towing_username,towing_fullname,towing_contactnumber)

这是我的代码的一部分

$q = "

SELECT  * , (
    6371 * acos (   
      cos ( radians($lat) )
      * cos( radians( towing_latitude ) )
      * cos( radians( towing_longitude ) - radians($lon) ) 
      + sin ( radians($lat) )
      * sin( radians( towing_latitude ) )
    )   ) AS distance FROM towing_list WHERE `towing_status`='$status' HAVING distance < $total_dis_miles   

ORDER BY distance LIMIT 0 , 20 ";

$r = mysql_query($q);

while ($row=mysql_fetch_object($r)) {  $array[]=$row; }

echo json_encode($array);

可以吗?我是 JSON 新手。请帮忙..

最佳答案

您可以使用加入

$q = "SELECT * , ( 6371 * acos (
cos ( radians($lat) ) * cos( radians( towing_latitude ) ) * cos(radians( towing_longitude ) - radians($lon) ) + sin ( radians($lat) ) * sin( radians( towing_latitude ) ) ) ) AS distance FROM towing_list INNER JOIN towing_info ON towing_info.towing_id = towing_list.towing_id WHERE towing_status='$status' HAVING distance < $total_dis_miles
ORDER BY distance LIMIT 0 , 20 ";

关于php - 从 MySQL 中的另一个查询调用查询并仅获取 1 个 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40022798/

相关文章:

java - 在 HQL 上加入声明

SQL Server错误: the multi-part identifier "C.Name" could not be bound

javascript - 编辑表单不显示任何数据并且在发布到数据库时无法保存

php - 使用 Instagram API 本地登录

javascript - 单击更改按钮并在数据库中记录更新,反之亦然

sql - 基于 WHERE IN 子句数据排序结果集

jquery - 使用 jQuery 将项目添加到空列表

php - 如何根据使用 mysql 和 php 输入的值插入重复值

php - Laravel 5.8 Eloquent 无法按预期工作,尽管它的原始转储查询有效

sql - DB2/SQL 相当于 SAS 的 sum(of ) 函数