mysql插入id比较两个表

时间:2015-04-14 14:33:23

标签: mysql insert compare

我有两张桌子: enter image description here

我想将table1中的post_id从table2插入media_id,其中两个表中的字段meta_value是相同的。有没有办法做到这一点?

2 个答案:

答案 0 :(得分:1)

是的,你可以使用join with join

来做到这一点
update table2 t2
join table1 t1 on t1.meta_value = t2.meta_value
set t2.media_id = t1.post_id

如果您想针对特定的meta_value进行此操作,可以在最后添加where条件

where t2.meta_value = '{your value}'

答案 1 :(得分:1)

我认为您可以使用此查询:

UPDATE table2
SET t2.media_id = t1.post_id
FROM table2 t2 inner join table1 t1 on (t1.meta_value = t2.meta_value);
相关问题