我有 2 个表word_lists
,memo_words
word_list 有很多 memo_words (1 - n)。
TABLE: word_lists
+----+----------+----------------+
| id | name | average_rating |
+----+----------+----------------+
| 1 | example1 | ? |
| 2 | example2 | ? |
| 3 | example3 | ? |
+----+----------+----------------+
ID is primary
TABLE: memo_words
+----+----------+-------+-------------------+
| id | name | rating| word_list_id |
+----+----------+-------+-------------------+
| 1 | random1 | 153 | 1 |
| 2 | random2 | 158 | 1 |
| 3 | random3 | 167 | 1 |
+----+----------+-------+-------------------+
ID is primary
我想为每个 word_lists 记录计算 word_lists.average_rating。平均评分 它是 memo_words.rating 中每个相关的 memo_words 记录的平均值。为了计算平均值,我可以简单地使用这样的东西:
SELECT id, AVG(rating) from memo_words group_by word_list_id;
但是我怎样才能更新 word_lists 记录呢?