2016-01-26 77 views
2

是否可以運行基於每行選擇的更新查詢?這裏就是我想嘗試:根據選擇更新MySQL表

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

但我不知道如何使選擇從廣告表採用正確的advert_id的每一行?

或者,這可能只是一個完全無效的語法,我不確定。

回答

1

只是簡單地指更新表:

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

我想這正是我一直在尋找。看起來很明顯,現在我想起它!我會在7分鐘內接受 –