MySQL Forums
Forum List  »  Portuguese

Subselect em Update
Posted by: Vinicius Souza
Date: September 16, 2021 01:05PM

Estou tentando realizar um update, onde para cada linha do update, seja feito um select com retorno randômico para alimentar as colunas da tabela do update.

Fiz a seguinte tentativa, mas sempre retorna o mesmo registro da segunda tabela para todos os registros selecionado da tabela do Update.

update table1 a1,
(select col1, col2
from table2
ORDER BY RAND() limit 1) a2
set a1.col1 = a2.col1, a1.col2 = a2.col2
where a1.col3 is not null;

exemplo estrutura
table1 | table2
id col1 col2 | id col1 col2
1 aaa bbb | 1 xxx yyy
2 ccc ddd | 2 www ttt
| 3 uuu vvv

Options: ReplyQuote


Subject
Views
Written By
Posted
Subselect em Update
578
September 16, 2021 01:05PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.