我需要一些幫助我查詢的優化SQL JOIN平均值
我有兩個表
表1 - users_location
; 表2 - users_rating
+-------+---------+ +-------+--------+
|user | location| | user | rating |
+-------+---------+ +-------+--------+
| user1 | AA | | user1 | 1 |
| user2 | BB | | user1 | 3 |
| user3 | CC | | user1 | 4 |
| user4 | AA | | user2 | 3 |
| user5 | BB | | user4 | 2 |
| user6 | CC | | user4 | 4 |
我需要選擇用戶和他們的平均評分位置(AA爲例)。 現在我的查詢是:
SELECT *
FROM users_loaction AS TABLE1
LEFT JOIN (
SELECT
user,
AVG(rating)
FROM users_rating group by user
) AS TABLE2
ON TABLE1.user = TABLE2.user
WHERE TABLE1.location = AA
我認爲SQL將所有用戶計算平均得分,然後加入只是我所需要的,這不是最優化。
謝謝你,你的決定非常好 – AlexCarlayl