简体   繁体   English

如何结合SELECT和UPDATE MySQL

[英]How to combine SELECT and UPDATE MySQL

I'm aggregating data and in a table and basically moving the data from one table to the next. 我正在将数据聚合到一个表中,并且基本上将数据从一个表移动到另一个表。

Currently, I'm selecting all the data from MySQL and then inserting it back into the new table. 目前,我正在从MySQL中选择所有数据,然后将其重新插入新表中。

Is there a way to combine this query to do it all within MySQL rather than taking the data out and then back in? 有没有一种方法可以组合此查询以在MySQL中完成所有操作,而不是先取出数据再取回?

Basically, I want to combine the following: 基本上,我想结合以下内容:

'''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)

You can JOIN the the query as a subquery in the UPDATE : 您可以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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM