sql >> Base de Datos >  >> RDS >> Mysql

MySQL combina dos columnas y agrega en una nueva columna

Crea la columna:

ALTER TABLE yourtable ADD COLUMN combined VARCHAR(50);

Actualizar los valores actuales:

UPDATE yourtable SET combined = CONCAT(zipcode, ' - ', city, ', ', state);

Actualizar todos los valores futuros automáticamente:

CREATE TRIGGER insert_trigger
BEFORE INSERT ON yourtable
FOR EACH ROW
SET new.combined = CONCAT(new.zipcode, ' - ', new.city, ', ', new.state);

CREATE TRIGGER update_trigger
BEFORE UPDATE ON yourtable
FOR EACH ROW
SET new.combined = CONCAT(new.zipcode, ' - ', new.city, ', ', new.state);