MySQL Appending between 2 tables - mysql

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

Related

MySQL table update after multi field check

Hi I have two table one like this one:
table1
and one like this:
table2
I would like to update all the fields on the table2 column "newID" based on this rules: if (table2.ID = table1.ID_actual or table2.ID=table1.ID_old) then table2.newID = table1.newID
How can I resolve this problem ?
You need a join of the 2 tables in the UPDATE statement:
UPDATE table2 t2
INNER JOIN table1 t1 ON t2.ID IN (t1.ID_actual, t1.ID_old)
SET t2.newID = t1.newID

MySQL: Copy values from one to another table

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

Mysql multi table delete effect all but one table

I used the following sql syntax to delete from several tables. The result is that the data is erased from table 2-4 but not in table 1 (no errors)
DELETE table1, table2, table3, table4 FROM table2
JOIN table1 ON table1.id = table2.deviceid
JOIN table3 ON table3.deviceid = table2.deviceid
JOIN table4 ON table4.device_id = table2.deviceid
WHERE table2.deviceid = 1;
What am I doing wrong?

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

Updating 2 tables in one query when similar data does not exist in both

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