Mysql
 sql >> Database >  >> RDS >> Mysql

MySQL combina due colonne e le aggiunge in una nuova colonna

Crea la colonna:

ALTER TABLE yourtable ADD COLUMN combined VARCHAR(50);

Aggiorna i valori correnti:

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

Aggiorna automaticamente tutti i valori futuri:

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);