MySQL update join with limit

后端 未结 3 2133
长情又很酷
长情又很酷 2021-02-15 06:05

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\

相关标签:
3条回答
  • 2021-02-15 06:21

    It's rather unconventional , but it should help you in a pinch

    SET @tmp_c = 0;
    UPDATE
        table1
    INNER JOIN table2 ON table1.id = table2.id
    INNER JOIN table3 ON table2.id = table3.id
    SET
        table1.remove_date = NOW() - INTERVAL 5 MONTH
    WHERE
        table1.active = 1
    AND
        (IF (table1.active = 1, @tmp_c := @tmp_c + 1 , 0 )) //This is to only increment the counter if the whole where matches
    AND
        @tmp_c <= 15 //Start ignoring row items as soon as it exceeds the limit amount
    
    0 讨论(0)
  • 2021-02-15 06:44

    sqlFiddle in this example I am only doing 5 rows, you can change the limit to 0,100 if you'd like

    UPDATE wp_postmeta,
          (SELECT oldmap,newmap
           FROM wp_map 
           ORDER BY oldmap,newmap limit 0,5)as wp_map
    SET wp_postmeta.meta_value = wp_map.newmap
    WHERE wp_map.oldmap = wp_postmeta.meta_value
    
    0 讨论(0)
  • 2021-02-15 06:46

    You can try it this way

    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;
    

    Here is SQLFiddle demo

    0 讨论(0)
提交回复
热议问题