代码之家  ›  专栏  ›  技术社区  ›  Adders

如何结合选择和更新MySQL

  •  0
  • Adders  · 技术社区  · 6 年前

    我在一个表中聚合数据,基本上把数据从一个表移到下一个表。

    目前,我正在选择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)
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Barmar    6 年前

    你可以 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