我有两张桌子:
films(id,marksNum)
marks(id,film_id,mark)
我想在films.marksNum
中为每部电影保存标记数量。
我的尝试是:
UPDATE films
SET marksNum=
(
SELECT count(id)
FROM marks
WHERE marks.film_id=films.id
GROUP BY marks.film_id
)
WHERE films.id=marks.film_id
但我发现错误:no such column: marks.film_id
我究竟做错了什么?
提前谢谢!
答案 0 :(得分:0)
嗯,这不是一个优雅的解决方案,但无论如何: 1)
CREATE TABLE `grp_marks` ( `film_id` INTEGER, `marks_num` INTEGER )
2)
INSERT INTO grp_marks(film_id,marks_num)
SELECT film_id,count(id)
FROM marks
GROUP BY film_id
3)
UPDATE films
SET marksNum=
(
SELECT marks_num
FROM grp_marks
WHERE (film_id=films.id)
)
WHERE EXISTS
(SELECT * FROM grp_marks WHERE grp_marks.film_id=films.id
)