Delete all row who are not in an UPDATE - mysql

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

Related

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 a field in 1 table where another field in that table = a field in another table

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.

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)

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

Mysql INSERT INTO from another table with condition and limit

I have two tables that have a one-to-many relationship table1 (one), table2 (many).
table1 has (t1_id) as KEY
table2 has (t2_id) as KEY and t2_t1_id as as refference to table1
Now in table1 I need a column (rand_t2_id) that holds an id from table2, I do not really care which one
This is the query I tried
INSERT INTO table1 (rand_t2_id)
SELECT t2_id
FROM table2
WHERE table1.t1_id = table2.t2_t1_id;
There also needs to be a limit build in somewhere if that is needed. I only need one id from table2
No luck here tho, anyone know a fix?
IMO you could try
UPDATE table1 t1 INNER JOIN table2 t2
ON t1.t1_id = t2.t2_t1_id;
SET t1.rand_t2_id = t2.t2_id
I assume you already have a column named rand_t2_id on your table1.
INSERT INTO table1 (t2_t1_id, rand_t2_id)
SELECT t2_id
FROM ( SELECT t2_id, t2_t1_id
FROM table2
ORDER BY RAND()) AS h
GROUP BY t2_t1_id
ON DUPLICATE KEY UPDATE rand_t2_id = VALUES(rand_t2_id)
I suppose that's what you're after?
This query would insert a random t2_id into relevant table1.