lundi 28 septembre 2020

Create a column in MySQL that automatically fills 0 or 1 based on presence of substring on previous column

I have this database at MySQL that has, among others, the following columns: Country, Level and Branch. I would like three new columns to this database indicating wether there's a comma (1) or not (0) on each of this three columns. Like this: example

I want these columns to be automatic, so new observations can be added to the table without the need of running the same script again and again to update the values shown there. I know how to do this in excel with IF functions, but I got stuck on MySQL. I have tried this:

alter table countries
add bol_coutnry int GENERATED ALWAYS AS (if(`COUNTRY` LIKE '%{$,}%',1));

among other variations of the same code but didn't even run. Any ideas? Thanks

Aucun commentaire:

Enregistrer un commentaire