MySQL Import only one column to existing table - mysql

I have the following problem.
I have a table which contains the following fields (example):
id, id2, id3, id4
Due to a mistake, I have accidently deleted all values of id3. (They are NULL now).
I have a second file (backup) which is a bit older, so it doesn't have the same count as the damaged table. The id3 is present there.
How do I "join" these tables, to restore at least the bigger part? Insert id3 ONLY to the damaged table from the non-damaged table?
I tried this:
UPDATE table1 SET `id3` = SELECT `id3` FROM table2
In my case, only phpMyAdmin or SQL Syntax solution (no console) would work...
Please help!

If we assume that one, two, or three of the ids define each row, then you can use join:
update table1 t1 join
table2 t2
on t1.id = t2.id
set t1.id3 = t2.id3;
This assumes that id is unique in the two tables. You might want to use a more complex expression:
update table1 t1 join
table2 t2
on t1.id = t2.id and t1.id2 = t2.id2 and t1.id4 = t2.id4
set t1.id3 = t2.id3;

Assuming id is an unique attribute.
You can simply run this query:
UPDATE table1 SET id3 = (SELECT id3 FROM table2 WHERE table2.id = table1.id)

Related

How to update a table from another one depending on certain conditions and getting data from a third one if the conditions are not met?

I need to update a table with values coming from another table while statisfying a few conditions in the meantime.
To be more specific, the 'source' field of table1 needs to get updated by the field 'value' from table2.
These 2 tables share a common 'id' field that can be used for a join. If the 'id' of table1 doesn't have any correspondence in table2, then it should take the value of the 'source2' field from table3. If the 'source2' value is 'NULL', then it should be given a default value that we will name 'Default' in this example.
As an example, we have the table1 below, which is the one we want to update:
Then we have the table2 below, which is the source table that will be used to update table1:
Finally we have table3, which will be used if the information in table2 is unavailable:
Based on this example, I would like to write a query that would update table1 with the values below:
I have written the following query using MariaDB but obviously it is not correct:
UPDATE table1 T1
LEFT JOIN table2 T2 ON T1.id = T2.id
LEFT JOIN table3 a ON T1.id = T3.id
SET T1.source = if(T2.value is NULL, if(T3.source is NULL, 'Default', T3.source), T2.value)
WHERE T1.id = T2.id
Which parts should be amended to make it work?
You seem quite close. The left join logic is fine, we just need to adjust the conditional set and the where clause:
update table1 t1
left join table2 t2 on t2.id = t1.id
left join table3 t3 on t3.id = t1.id
set t1.source = coalesce(t2.value, t3.source, 'Default')
where t2.id is not null or t3.id is not null
coalesce() returns its first non-null argument.
The where clause ensures that we don't update rows that match in neither tables. You can remove it if you want to update all rows (those that do not match will get 'Default' assigned).

Mysql - updating and insert using select * using a target column

update table1 t1
inner join
table2 t2 on
t1.a = t2.a
set t1.b = t2.b,
t1.c = t2.c;
This code works to join 2 tables on column a. My problem is that I have about 500 columns which I want to update and am currently writing out each of the 500 columns in the code up to
t1.500 = t2.500;
This works, but it is slow and inefficient. Does anyone know how you can select * from table2 to update table1, keeping the join on t1.a = t2.a? All of the column names match exactly and am inserting all of the columns from table2. Was thinking of something like this below although I know that this is not correct. Thank you!
update table1 t1
inner join
table2 t2 on
t1.a = t2.a
set t1.* = t2.*;
I think there is no way to make update query with a wildcard in mysql. Don't know if it will properly fit to your problem, but you can try this workaround. :
DELETE FROM table2 WHERE id IN (<ids>);
Delete all the records from table2 that are satisfying given condition. And then insert the corresponding records from table1 to table2.
INSERT INTO table2
SELECT * FROM table1 WHERE id IN (<ids>);

MySQL - update whole column with records from parent table?

I have two tables t1, t2 that I have created and loaded data from a CSV into these.
I had to then create a new PK column as the existing columns (t1.old_id, t2.old_id) are strings that would naturally be a PK are not absolutely fixed (this seems to be advised against?)
so I created a id PK INT AUTO_INCREMENT in each table
as one record in t1 is linked to many in t2 and I want to maintain referential integrity between these two tables.
I believe what i need to do is create an id INT NOT NULL in t2 as an FK
This t2.id is blank at the moment (as it is dependent ont1.id`)
Am I right in thinking I need an UPDATE query with a JOIN of some description to make this work?
The following produces the data exactly that I want to update into my t2.id column - but I don't know how to do the update
select t1.id
from t1
inner join t2
on t1.old_id = t2.old_id
You can use a join in your UPDATE statement like this:
UPDATE t2
JOIN t1 ON t1.old_id = t2.old_id
SET t2.id = t1.id
You can use a correlated UPDATE query like this
UPDATE t2
SET id = (SELECT MAX(t1.id) FROM t1 WHERE t1.old_id = t2.old_id);
*Assuming you have a single t1.id for each t1.old_id
On a Separate Note, You should name t2.id like t2.t1ID so as to remove ambiguity if and when you have a identity column in t2 as well named id

MySql Query If Field equals Field in different table update different field

I have got two tables. I want to update MODEL in table2 when ITEM in table1 equals ITEM in table2.
Any Ideas?
In MySQL, you do it like this
UPDATE table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
SET t1.col1 = t2.col1,
t1.col2 = t2.col2
If I understand correctly, you just want to perform an UPDATE on table2 based on, presumably, foreign keys?
If that's right, this should work:
UPDATE
table2
JOIN table1
ON table1.ITEM = table2.ITEM
SET
MODEL = 'new value';
The table declaration in an UPDATE statement is the same as is specified in a SELECT statement - so you can use any type of JOIN that fits your table/data.
Docs for UPDATE, SELECT.
If you could add an actual query attempt, or something, that might be helpful. Can you try something like the following:
UPDATE table2 JOIN table1 ON table2.ITEM = table1.ITEM SET MODEL = ?

Getting table data excluding constraints

Table1 - has constraints with Table2 & Table3
Table2
Table3
Any data which is present in table1 with constraints with table 2 & 3 is valid data.
There are some bogus data somehow entered in table1 by manually turning off the constraint.
I want to collect those data which is present only in table1 without any constraints.
Is there an easy way to get table1 data in mysql which don't have constraint data attached to it?
Thanks.
If I understand your question, you are looking for missing or bogus records in the parent table. I'm going to imagine that the constraint field in table2 and table3 is id and the fk fields in table1 are table2_id and table3_id. If this is the case, you'd query for missing joins:
SELECT t1.id, t1.table2_id, t1.table3_id FROM table1 t1
LEFT JOIN table2 t2 ON t2.id = t1.table2_id
LEFT JOIN table3 t3 ON t3.id = t1.table3_id
WHERE t1.table2_id IS NULL
OR t1.table3_id IS NULL;