如何编写这个 SQL 子查询? [英] How to write this SQL sub-query?

查看:22
本文介绍了如何编写这个 SQL 子查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的这个查询运行良好

从这个查询中,我选择了距离我所在位置 3 公里的所有餐厅,这是我的第一张桌子.

From this query I am selecting all restaurant 3 KM from my location this is my 1st table.

SELECT foodjoint_id,foodjoint_name,open_hours,cont_no,address_line,city ( 3959 * acos( cos( radians('".$userLatitude."') ) * cos( radians( foodjoint_latitude) ) * cos( radians( foodjoint_longitude) - radians('".$userLongitude."') ) + sin( radians('".$userLatitude."') ) * sin( radians( foodjoint_latitude) ) ) ) AS distance
FROM provider_food_joints
HAVING distance < '3' ORDER BY distance LIMIT 0 , 20

但我需要从这 3Km 内的那些食品联合中选择 AVG 评级.

But I need to select the AVG rating from those food joint which are with in this 3Km.

查询也运行良好:

select AVG(customer_ratings) from customer_review where foodjoint_id=".$foodjoint_id 

但我需要添加这两个查询,通过它我可以选择所有这些食品联合及其评级 AVG.

but I need to add this two query through which I can select all those food joint and their rating AVG.

推荐答案

只要放置子查询,你就会得到你的结果:

Just place the subquery and you will get your result:

`SELECT foodjoint_id,foodjoint_name,open_hours,cont_no,address_line,city ( 3959 * acos( cos( radians('".$userLatitude."') ) * cos( radians( foodjoint_latitude) ) * cos( radians( foodjoint_longitude) - radians('".$userLongitude."') ) + sin( radians('".$userLatitude."') ) * sin( radians( foodjoint_latitude) ) ) ) AS distance,

(select AVG(customer_ratings) from customer_review where customer_review.foodjoint_id=provider_food_joints.foodjoint_id) as Customer_Reviews

 FROM provider_food_joints 

HAVING distance < '3' ORDER BY distance LIMIT 0 , 20`

这篇关于如何编写这个 SQL 子查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆