例如,我需要为SQL中的每一行获取AVG:
这是第一张桌子
+ ---+------+-------------+ | course_id | course_name | + ----------+-------------+ | 1 | a | | 2 | b | | 3 | c | | 4 | g | + ---+------+-------------+
This is the second tableI need to get AVG for both id 1 and 2. the result for example:
+ -------------------+------+----------+ | course_feedback_id | rate |course_id | + -================--+------+----------+ | 1 | 4 | 1 | | 2 | 3 | 1 | | 3 | 2 | 2 | + -------------------+------+----------+
this is the final answer that i need
+ ----------------------+ | course_id | AVG(rate) | + -=======--+-----------+ | 1 | 3.5 | | 2 | 2 | + ----------------------+
I tried this soulution but it will give me only the first row not all records.
SELECT *, AVG(`rate`) from secondTable
请帮忙
最佳答案
尝试这个:
SELECT c.course_id, AVG(fb.rate)
FROM course AS c
INNER JOIN course_feedback AS fb ON fb.course_id = c.course_id
GROUP BY c.course_id