Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INSERT data from one table to another where ids match

Tags:

mysql

I have two tables:

1. `blog_export`: 'id', 'body'. 
'id' already has values 'body' is empty. 

2. `field_data_body`: 'body_value','entity_id' 

I would like to copy body_value from field_data_body and insert that data into the column 'body' on the table blog_export but ONLY where 'id' matches 'entity_id'

I have the statement

INSERT INTO `blog_export` (`body`)
SELECT `body_value`
FROM `field_data_body`
WHERE `bundle` = 'wp_blog' AND `entity_id` = `blog_export`.`id`

but it doesn't work. How do I do this?

like image 586
LTech Avatar asked Dec 01 '25 05:12

LTech


1 Answers

You need to perform an UPDATE operation instead joining with other table like

UPDATE `blog_export` be
JOIN `field_data_body` fdb ON fdb.`entity_id` = be.`id`
SET be.`body` = fdb.`body_value`;
like image 122
Rahul Avatar answered Dec 03 '25 19:12

Rahul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!