MySQL 更新加入限制

2022-01-05 00:00:00 join sql mysql phpmyadmin

我想知道如何使用另一个表中的值更新表,诀窍是我需要设置一个限制,因为我有数千行要更新,而 PHPmyadmin 无法处理该负载.(我没有直接访问服务器的权限)

I would like to know how to update a table, with values from another table, by the trick is I need to set a limit because I have thousands of rows to update, and PHPmyadmin can't handle that load. ( I dont have direct access to the server )

我的表结构是这样的

wp_postmeta

meta_id,post_id,元密钥,元值

wp_map

旧地图,新地图

我需要做的是将 wp_postmeta.meta_valuewp_map.oldmap 上的两个表连接起来,并更新 wp_postmeta.meta_valuewp_map.newmap.

What I need to do is join the two tables on wp_postmeta.meta_value and wp_map.oldmap, and update the wp_postmeta.meta_value with wp_map.newmap.

这是我当前的查询,但我需要向查询添加一个 LIMIT 为 100,因为我将查询拆分为更小的块,以便 PHPMyAdmin 可以处理.

Here is my current query, but I need to add a LIMIT of 100 to the query, as I'm splitting the query up into smaller chunks so PHPMyAdmin can process.

UPDATE wp_postmeta
INNER JOIN wp_map
ON wp_map.oldmap = wp_postmeta.meta_value
SET wp_postmeta.meta_value = wp_map.newmap;

我阅读了有关创建子查询的文章,但找不到任何相关示例,因此,如果有人能指导我朝着正确的方向发展或提供一个可行的示例,我们将不胜感激.

I read about creating a subquery, but couldn't find any relevant examples, so if someone could steer me in the right direction or provide a working example it would be greatly appreciated.

推荐答案

你可以这样试试

UPDATE wp_postmeta t JOIN
(
    SELECT p.meta_id, m.newmap
      FROM wp_postmeta p JOIN wp_map m
        ON p.meta_value = m.oldmap
     ORDER BY p.meta_id
     LIMIT 100
) s
   ON t.meta_id = s.meta_id
  SET t.meta_value = s.newmap;

这是SQLFiddle 演示

Here is SQLFiddle demo

相关文章