Update with select concatenated between two columns of the same mysql table

Asked

Viewed 1,940 times

1

I am trying to run a query in which I will concatenate two columns of mysql name and surname, and update this concatenation to the full name variable in all table records.

Follows the code:

UPDATE eskalera.curriculum
SET 
nomecompleto = (

SELECT CONCAT(nome, ' ', sobrenome) 

from eskalera.curriculum
) 
  • You’re missing a WHERE there, no?

  • 1

    @Ingridfarabulini precisely my doubt because the query is for all records as it would be correct to solve the problem?

1 answer

1


The common way is to use UPDATE:

UPDATE eskalera.curriculum SET nomecompleto = CONCAT(nome,' ',sobrenome);

Thus this query will be able to make the change in all table records in a single transaction. But for this it is necessary to be with the protection against unwanted updates throughout the disabled table:

SET SQL_SAFE_UPDATES=0; 

Then we would have:

SET SQL_SAFE_UPDATES=0; 
UPDATE eskalera.curriculum SET nomecompleto = CONCAT(nome,' ',sobrenome);

At the end, it would be interesting to activate the protection again:

SET SQL_SAFE_UPDATES=1; 

Thus protection against unwanted updates at once throughout the table is reactivated.

  • 1

    Thank you very much, thank you for the answer you gave!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.