One request for update and select internal connection in mysql

How to combine these two queries?

select mysql query:

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

update mysql query:

update table institutions 
set averageRatings=avg(b.averageRating) 
where id=a.institudeId;
+4
source share
1 answer

You can try this query:

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'
)
+6
source

Source: https://habr.com/ru/post/1695691/


All Articles