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

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>);

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 Import only one column to existing table

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)

Delete all row who are not in an UPDATE

I have a table that i wanna UPDATE with another table so i have something like :
UPDATE table1 JOIN table2 ON table1.id = table2.fk_table1 SET table1.field1 = table2.field1
But i also want to delate all the row from this table who are not in this UPDATE for have something like :
DELETE FROM table1 WHERE id not IN (UPDATE ...)
Their is a way to do that in one optimize sql request or i have to do it in two request?
Thanks
You have to do it in two request as they are two different operation DML operations:
First fire your update statement:
UPDATE table1 JOIN table2 ON table1.id = table2.fk_table1 SET table1.field1 = table2.field1
Then fire your delete request by converting update in select:
DELETE FROM table1 WHERE id not IN (Select...)
Note: Use select with same condition in update command to get the list of records which are updated in first statement.
You can use below query-
DELETE FROM tbl1.* FROM
table1 AS tbl1
LEFT JOIN table2 AS tbl2 ON table1.id = table2.fk_table1
WHERE tbl2.id IS NULL
But it will be slow as per table size and create locking so you should do that first fetch all id from table1 and then delete them.
SELECT tbl1.id FROM
table1 AS tbl1
LEFT JOIN table2 AS tbl2 ON table1.id = table2.fk_table1
WHERE tbl2.id IS NULL
Now delete these ids either put in clause or multiple chunks as per no of records.
DELETE FROM table1 WHERE id IN ();

Delete all records from one table where they match criteria from two (or more) tables

Consider the following:
QUERY
SELECT * FROM
`table1`,`table2`
WHERE `table1`.`RemoteID` = `table2`.`ID`
AND `table2`.`UserID`=1
How can I change it from a SELECT to DELETE from table1 where these records match? It must only delete from table1, not table2
In less specific terms, I want to delete all records from table1 where they match some criteria of both tables (discretely and relatively)
You can use IN with sub query
DELETE FROM table1
WHERE `table1`.`RemoteID` IN (
SELECT ID
FROM table2
WHERE `table2`.`UserID`=1)
Try this,
Delete
from table1
where Id in
(select table1.Id
from table1 t1, table2 t2
where t1.RemoteID = t2.ID
AND table2.UserID = 1)

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 = ?