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
Related
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.
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';
I'm trying to update multiple tables in a single query, but what I using does not seem to do any updating.
UPDATE table1,table2 SET table1.name='John Doe',table2.name='John Doe'
WHERE table1.id=1 and table2.id = 1;
Problem is, a row with the same id may not be present in both tables. How can I do an update in a case like this?
In this case, the id 1 is present in table1, but not in table2.
EDIT
The idea is the update data in both tables, even if an id does not exist in table1 or table2
Example:
id is present in table1 but not in table2 -> Update table1.
id is present in table2 but not table1 -> update table2.
id is present in both table1 and table2 update both.
id is not present in either tables -> do nothing
Try this,
UPDATE table1
LEFT JOIN table2
ON table1.id = table2.id
SET table1.name = 'John Doe',
table2.name = 'John Doe'
WHERE table1.id = 1
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;
I need to do a Appending on MySql between two tables, my first table1 get all data but for the table2 i have only EMAIL :
table1 (ID;FIRSTNAME;LASTNAME;EMAIL;BIRTH;CP) 100 000 Rows
table2 (ID;FIRSTNAME;LASTNAME;EMAIL;BIRTH;CP) 1 000 Rows
Exemple
Table1 :
1;JOHN;DOE;john.doe#gmail.com;1981-06-06 00:00:00;92220
Table2 :
NULL;NULL;NULL;john.doe#gmail.com;NULL;NULL
and I want to UPDATE on table2 all the columns to have this match :
1;JOHN;DOE;john.doe#gmail.com;1981-06-06 00:00:00;92220
Use the multiple-table UPDATE syntax to join the tables on EMAIL and set the fields appropriately:
UPDATE table2 JOIN table1 USING (EMAIL) SET
table2.ID = table1.ID,
table2.FIRSTNAME = table1.FIRSTNAME,
table2.LASTNAME = table1.LASTNAME,
table2.BIRTH = table1.BIRTH
table2.CP = table1.CP