生成的列 condition/if else/case 以填充新列 mysql

Generated columns with condition/if else/case to populate the new column mysql

我有一个列-城市,我想创建一个新列-地铁,我想让它检查 如果城市在('x','y','z') then Metro='Metro' else 'Non metro'

   ALTER TABLE sales ADD COLUMN Metro_city VARCHAR(45) GENERATED ALWAYS AS  (IF (CITY in('Mumbai ','Delhi' , 'Bangalore') then Metro_city='Metro') ('Non-Metro' end if)) STORED;

我还有另一组值要放入案例中,但我猜如果我能完成这项工作,那也会。我希望它存储到 table 而不是 select 查询

您需要修复 case 表达式,基本上:

ALTER TABLE sales ADD COLUMN Metro_city VARCHAR(45) GENERATED ALWAYS AS
      (case when CITY in ('Mumbai ', 'Delhi' , 'Bangalore')
            then 'Metro'
            else 'Non-Metro'
       end ) STORED;