1
我想出瞭如何一次執行一個字段(請參閱下面的兩個更新查詢)。有沒有辦法在單個查詢中做到這一點?我可以在Postgres中使用來自其他表的彙總數據更新表中的多個字段嗎?
UPDATE upload_data sd
SET
photo_count =
(SELECT sum(photo_count)
FROM media_uploads mu
WHERE mu.user_id=sd.user_id
AND mu.date=current_date
GROUP BY mu.user_id);
UPDATE upload_data sd
SET
video_count =
(SELECT sum(video_count)
FROM media_uploads mu
WHERE mu.user_id=sd.user_id
AND date_trunc('month', mu.date)=date_trunc('month', sd.date)
GROUP BY mu.user_id);