How to write this SQL sub-query?

2019-07-09 09:00发布

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.

标签: sql subquery
1条回答
趁早两清
2楼-- · 2019-07-09 09:45

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`
查看更多
登录 后发表回答