Update table with data from a second table - mysql

I have one table with the following columns:
ID, title
I need to update every title cell in this table with the title value from another table that has this structure:
ID, attribute, value
The problem being that table 2 above could have any number of attribute types (title, location, url), and I only want the title attribute copied. I have tried the following but it fails:
UPDATE table1
SET table1.title = table2.value
where table2.attribute='title' and table1.ID = table2.ID;
Any ideas? Thanks in advance.

Use join
UPDATE table1 t
JOIN table2 t2 ON t.ID = t2.ID
SET t.title = t2.value
WHERE t2.attribute='title';

Related

copy columns between two tables without inserting new rows

table1 - columns:
id,
date,
table2 - columns:
id,
date,
img,
auth,
moder,
tags,
visits
table1 - 1200 rows
table2 - 1000 rows
I want to copy missing columns from table2 to table1 (img, auth, moder, tags, visits).
regardless of values (this are all test data, doesn't matter what value is on a certain row).
I tried firstly to create missing columns in table1 and then:
insert into table1 (img, auth, moder, tags, visits) select img, auth, moder, tags, visits from table2;
But this inserts new rows into table1. I don't want new rows - just add new columns having data from table2.
It looks like what you want here is an update, not an insert, since you don't actually want to add new records, just modify ones which already exist.
UPDATE table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id AND t1.date = t2.date
SET
t1.img = t2.img,
t1.auth = t2.auth,
t1.moder = t2.moder,
t1.tags = t2.tags,
t1.visits = t2.visits;
use update
update table1
join table2 on table1.id=table2.id and table1.date=table2.date
set tabl1.img=tabl2.img, table1.auth=table2.auth, table1.moder=table1.moder,
table1.tags=table2,tags, table1.visits=table2.visits

Append data from first MySQL table to second by ID

I have two MySql Databases.
One has:
___id___|___name___|__date____|
Second database has
___id___|___tag___|
One common thing in this tables - id. It's same in both tables. How can i append column "tag" to first mysql database?
If you just want a view of the columns in the first table along with a possible matching tag, then use a query:
SELECT
t1.id, t1.name, t1.date, t2.tag
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id;
If you instead want to actually add a new tag column to the first table, then add that column and then do an update:
ALTER TABLE table1 ADD COLUMN tag VARCHAR(55);
UPDATE table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
SET t1.tag = t2.tag;

mysql - update a field in 1 table where another field in that table = a field in another table

Need help with a mysql query. I have 2 tables - table1 and table2. I am trying to update a field in table1 which is not included in table2 - named Status. And I want to update that field with the value of 'A'. table1 and table2 DO have a field in common - named Member_ID. Here is my query that is giving me errors:
UPDATE table1 SET Status='A' WHERE Member_ID=table2.Member_ID;
Is there some type of join that is needed? Any help would be appreciated. Thanks.
cdr6800
This can be done with exists
UPDATE table1 s
SET s.status = 'A'
WHERE EXISTS(select 1 from table2 t
WHERE t.member_id = s.member_id)
You have to JOIN table1 to table2 like this:
UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.Member_ID = t2.Member_ID
SET t1.Status='A'
This will update all table1 records having a Member_ID value that also exists in table2.

Copy field from one table to another where fields match

I have two tables that I need to import data from where both share a column with the same data. However one table has a value in it that needs to be copied to another. I need an sql command that works like below:
Update table1 contenttitle with table2 title where table2 id equals table1 contentid
So essentially it is copied the values from table2 title and inputting them in table 1 contenttitle where the values in table2 id and table1 contentid are equal.
update table1
join table2 on table1.contentid = table2.id
set contenttitle = table2.title
Update table1
set table1.content1=table2.content2
from table2 join table1 on table2.contentid=table1.contentid
update table1
set table1.contenttitle = table2.title
FROM table1
inner join table2 on table1.id= table2.id

update one column with data from other column different table

I want to update a column from one table to the column in another table. Both columns have the same name and same ID and are populated. I need to update only IDs which match in both tables ( column id)
So for example all values from table_1 in column_x to be copied to table_2 in column_x if both are with same column_id
Fixed with help from other site.
tried: UPDATE table1 JOIN table2 ON column1.column_x = table2.column_x SET table2.id = table1.id;
It looks like you've mixed up which column to join on and which column to copy. Your stated problem is:
all values from table_1 in column_x to be copied to table_2 in column_x if both are with same column_id
The query to do this would be the following:
UPDATE table1 JOIN table2 ON table1.column_id = table2.column_id
SET table2.column_x = table1.column_x;
Or slightly more concisely:
UPDATE table1 JOIN table2 USING (column_id)
SET table2.column_x = table1.column_x;