问题
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