替换多个值 - MySQL

时间:2014-03-22 12:40:46

标签: mysql sql database phpmyadmin

       table 1                                     table 2

+---------+------------+                    +---------+------------+
| post_id | meta_value |                    |    ID   | post_title |
+---------|------------+                    +---------|------------+
|    1    |   value_a  |                    |    1    | title_abc  |
+---------|------------+                    +---------|------------+
|    2    |   value_b  |                    |    2    | title_xyh  |
+---------|------------+                    +---------|------------+
|    3    |   value_c  |                    |    3    | title_jer  |
+---------|------------+                    +---------|------------+
| .....   | .........  |                    |   ..... |   .......  |
+---------|------------+                    +---------|------------+
|   999   | value_xyzw |                    |   999   | title_bhw  |
+---------|------------+                    +---------|------------+

我有2张桌子。我尝试将post_title(表2)中的记录替换为meta_value {表1)。

示例:

value_a替换title_abc

value_b替换title_xyh

value_c替换title_jer

............

谢谢!

2 个答案:

答案 0 :(得分:1)

您想要update join

update table1 t1 join
       table2 t2
       on t1.post_id = t2.id
    set t1.meta_value = t2.post_title;

答案 1 :(得分:1)

UPDATE table1, table2 SET table1.metavalue = table2.post_title 
WHERE table1.id = table2.id