SQL Join表获取平均值

时间:2016-08-23 18:58:25

标签: mysql sql

我有以下表格:

this.pictureBoxWarning.Image = System.Drawing.SystemIcons.Warning.ToBitmap();

+--------+ +----------------+ +------------+ | Rating | | Rating_Person | | Person | +--------+ +----------------+ +------------+ | ID | | RAT_ID | | ID | | RATING | | PER_ID | | AVE_RATING | +--------+ +----------------+ | NO_RATINGS | +------------+ 可以包含多个Person

我有一个SQL查询,我得到一个Ratings列表:

Persons

问题:

如何加入SELECT * FROM PERSON 表格以填充RatingAVE_RATING值?

我可以对NO_RATINGS表执行以下操作,但如何计算JOINAVE_RATING

NO_RATINGS

感谢您的帮助。

(我正在使用MySQL)

1 个答案:

答案 0 :(得分:0)

解决方案:

SELECT 
count(R.ID) as NO_ROWS, avg(R.RATING) as AVE_RATING,
P.* FROM PERSON P 
INNER JOIN rating_person RP ON P.ID = RP.PER_ID
INNER JOIN RATING R ON RP.RAT_ID = R.ID
group by P.ID