How to write this SQL sub-query?

感情迁移 提交于 2019-12-24 02:57:17

问题


I have this query which is running perfectly

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

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

The query is also running perfectly:

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

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


回答1:


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`


来源:https://stackoverflow.com/questions/10461209/how-to-write-this-sql-sub-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!