2012-05-05 136 views
2

我有這個查詢是完美運行如何編寫這個SQL子查詢?

從這個查詢我選擇從我的位置的所有餐廳3 KM這是我的第一張桌子。

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評級。

查詢也完美運行:

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

,但我需要添加這兩個查詢,通過它我可以選擇所有這些食品關節和他們的等級AVG。

+3

歡迎計算器:-)突出你的查詢,然後單擊花括號,第五個按鈕從**乙** **我開始** ... –

+0

作爲一個額外的文體筆記,你不應該使用整數週圍的引號('3'爲距離)。如果您或其他人返回並查看查詢,這是誤導性的。 –

回答

1

只需將子查詢,你會得到你的結果:

`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` 
+1

非常感謝 – JANNURM

+0

我的榮幸先生。 –