D
D
Dmitry2019-10-01 23:46:05
SQL
Dmitry, 2019-10-01 23:46:05

Zeroing a table field if the value from another field is not in the second table?

There are two tables
table1 (about two million records):
id name state
table2 (about 50 thousand records):
name
each has many columns, but only those are of interest.
Two questions:
1. The bottom line is that in table1 you need to periodically set state to NULL if name is not in table2. How can this be done? According to SQL, I remember only the very, very simple basics, and googled only confuses.
2. How can you reduce the load on the database as much as possible (that is, do not lock it for a long time)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nozzy, 2019-10-02
@DmitryMry

update table1 t1
left join table2 t2 on t2.`name` = t1.`name`
set t1.`name` = null
where t2.`name` is null

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question