根据select更新MySQL表

时间:2016-01-26 20:39:30

标签: mysql

我是否可以根据每行的选择运行更新查询?这就是我在想的尝试:

UPDATE adverts SET advert_visits = (SELECT COUNT(earning_id) FROM earnings WHERE earning_advert_id = 1093)

但是我不确定如何让select从广告表中为每一行应用正确的advert_id?

或许这只是一种完全无效的语法,我不确定。

1 个答案:

答案 0 :(得分:1)

只需参考更新的表格:

UPDATE adverts
SET advert_visits = (
    SELECT COUNT(earning_id)
    FROM earnings
    WHERE earning_advert_id = adverts.advert_id
)