单个查询更新并在mysql中选择内连接

时间:2018-04-03 10:35:14

标签: php mysql

如何组合这两个查询?

选择mysql查询:

SELECT avg(b.averageRating) 
FROM review a INNER 
JOIN review b ON b.institudeId=a.institudeId 
WHERE a.reviewId='3';

更新mysql查询:

update table institutions 
set averageRatings=avg(b.averageRating) 
where id=a.institudeId;

1 个答案:

答案 0 :(得分:6)

您可以尝试此查询:

UPDATE institutions SET institutions.overallRatings = (
  SELECT avg(b.averageRating) 
  FROM review a 
  INNER JOIN review b ON b.institudeId=a.institudeId WHERE a.reviewId='3'
) 
WHERE institutions.id = (
  SELECT institudeId FROM review WHERE reviewId='3'
)