MySQL 更新加入限制 [英] MySQL update join with limit

查看:25
本文介绍了MySQL 更新加入限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用另一个表中的值更新表,诀窍是我需要设置一个限制,因为我有数千行要更新,而 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

这篇关于MySQL 更新加入限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆