[英]How to combine SELECT and UPDATE MySQL
我正在將數據聚合到一個表中,並且基本上將數據從一個表移動到另一個表。
目前,我正在從MySQL中選擇所有數據,然后將其重新插入新表中。
有沒有一種方法可以組合此查詢以在MySQL中完成所有操作,而不是先取出數據再取回?
基本上,我想結合以下內容:
'''update landing_pages_v3 set mobile_first_place_rankings=%s where id=%s''', data
'''select count(keyword), lp_id from keywords_v3 where profile_id=%s and device=%s and positions=1 group by lp_id''', (profile_id, device)
您可以JOIN
的查詢作為子查詢UPDATE
:
UPDATE landing_pages_v3 AS l
JOIN (
SELECT count(*) AS ct, lp_id
FROM keywords_v3
WHERE profile_id=%s AND device=%s AND positions=1
GROUP BY lp_id
) AS k ON l.id = k.lp_id
SET mobile_first_place_rankings = k.ct
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.