A
A
AndreyBerezov2015-12-19 14:33:28
PHP
AndreyBerezov, 2015-12-19 14:33:28

How to make a complex MySQL UPDATE query to partially merge data from two tables?

Good day!
Tell me how to correctly make a query to update data if there are 2 tables, each of which has user_id and unicnumber fields. You need to take a pair of table1.user_id->table1.unicnumber (1 to 1) from one table and copy the unicnumber value to all records of another table, where table1.user_id matches table2.user_id. It will turn out table2.user_id->table2.unicnumber (Many to one).
The query for getting from the first table is: SELECT user_id, unicnumber FROM table1 WHERE accept = 1;
An example of an update request to another table is: UPDATE table2 SET unicnumber = 1234 WHERE table2.user_id = 1;
It is possible to execute 100,000 requests in a PHP loop, forming them based on the data received from the first request. But maybe there is a more elegant solution using MySQL?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2015-12-19
@AndreyBerezov

update table2 
join table1 using(user_id)
 set table2.unicnumber = table1.unicnumber 
where table1.accept = 1

?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question