[英]SQL: Update multiple rows with result from same table
I'm trying to update a field (post_id) in multiple rows of a table with a value available in one row of the same table. 我正在尝试使用同一表的一行中的可用值更新表的多行中的字段(post_id)。 The table is a Wordpress table wp_meta
该表是一个WordPress表wp_meta
Here is the structure of the table 这是表格的结构
meta_id | post_id | meta_key | meta_value
1 16 author "Staff"
2 16 title "title"
3 16 old_id 250
4 17 author "jay"
5 17 old_id 251
6 18 author "panda"
7 18 title "realy nice"
8 18 old_id 252
9 18 some_key lorem"
I want this to be so that the post_id should be replaced with the old_id for that corresponding set of posts. 我希望这样,以便将post_id替换为该对应帖子集的old_id。 The result should be something like this
结果应该是这样的
meta_id | post_id | meta_key | meta_value
1 250 author "Staff"
2 250 title "title"
3 250 old_id 250
4 251 author "jay"
5 251 old_id 251
6 252 author "panda"
7 252 title "realy nice"
8 252 old_id 252
9 252 some_key lorem"
This is what I'm trying, but it's wrong 这是我正在尝试的方法,但这是错误的
UPDATE wprk_postmeta as t1
SET post_id = (SELECT t2.meta_value from wprk_postmeta as t2 WHERE t2.meta_key='old_id' AND t2.post_id=t1.post_id)
When I run the above I get sub query returned more than one value error. 当我运行上面的代码时,我得到的子查询返回了多个值错误。
I think you want a join
: 我想你想
join
:
UPDATE wprk_postmeta pm JOIN
work_postmeta pmo
ON pm.post_id = pmo.post_id AND
pmo.meta_key = 'old_id'
SET post_id = pmo.meta_value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.