The problem is the syntax you use.
You can insert the row with the columns num
and rat
in a single INSERT
statement:
INSERT INTO avgg(num, rat)
SELECT 'The average is', AVG(rating)
FROM ratings
WHERE movie_id IN (SELECT id FROM movies WHERE year = '2012');
Or with a join:
INSERT INTO avgg(num, rat)
SELECT 'The average is', AVG(r.rating)
FROM ratings r INNER JOIN movies m
ON m.id = r.movie_id
WHERE m.year = '2012';
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…