I have two almost identical database tables with the presice same colummns and number of rows in them.
They both have a unique ID per row which is the same in both tables.
The first table is missing some values on some columns.
How can i copy values from a row in table2, to row in table1?
The columns have the same name, but the value is empty in table1, but not table2. I want to copy over all values from some columns in table2 to same columns in table1.
It's a big table with over 1 million rows.
Example:
Table 1 Row 5:
id = 5
orgnr = 932942
homepage = NULL
name = NULL
Table 2 Row 5:
id = 5
orgnr = 932942
homepage = 'www.domain.com'
name = 'John Smith'
I want to copy values from homepage and name to table1's columns.
Thanks in advance.
In MySQL you can use joins to update a table with values from another table:
UPDATE table1 t1
JOIN table2 t2
ON t1.id = t2.id
SET t1.homepage = t2.homepage,
t1.name = t2.name
Dirty custom query that requires you to add all fields but could do the trick:
UPDATE table1
SET
field1 = ISNULL(t1.field1, t2.field1),
field2 = ....
FROM
table1 t1
INNER JOIN table2 t2 ON t1.Id = t2.Id
If updating the 1m rows in one go is too much you can try to do it in batches by using a where clase:
WHERE
t1.Id BETWEEN #batchStart AND #batchEnd
Related
I have a table which contains unique names, call it table1. I have another table which contains the same names but each name occurs several times, call it table2. Now, I want to copy data from table2 to table1 corresponding to the names. And if table2 has multiple records by the same name, I want the corresponding new records to be created in table1.
TABLE1 TABLE2
NAME NAME
A A
B A
C B
D B
Following the little chat int he comments, you could try this:
UPDATE t1
set columnx = t2.columnx
FROM table1 t1
LEFT JOIN table2 t2 on t2.name = t1.name
WHERE t2.name is null
To achieve your full requirements, you may find it more useful to have multiple queries accomplish the one task.
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
Trying to create a mysql query that will do the following:
For each record within Table 1, find a matching record in Table 2 where column A & B of Table 1 match column Y & Z of Table 2.
Once a match is found, grab column X value from Table 2 record and insert that value into column C of the original record in Table 1.
I hope that makes sense.
How the heck do I do that?
Use a multi-table update, just without modifying any columns from Table 2, as follows:
UPDATE Table1 T1, Table2 T2
SET T1.C = T2.X
WHERE T1.A = T2.Y AND T1.B = T2.Z;
UPDATE table1 INNER JOIN table2 ON table1.a = table2.y AND table1.b = table2.z SET table1.c = table2.x;