Get the App
SLTechnology News&Howtos  ›  Database  › 

Add a column according to the field condition

Shulou Source: shulou.com Published: 2022-06-01 11:30:39 11月16日 Update

There is a table student that has the following structure:

Sid sname ssex = 1 Zhang San 1 2 Li Si 1 3 Liu Hong 0 4 Yang Jing 0 5 Li Xing 1

The requirement to write a sql statement shows the following effect:

Sid sname ssex gender = 1 Zhang San 1 male 2 Li Si 1 male 3 Liu Hong 0 female 4 Yang Jing 0 female 5 Li Xing and 1 male

Solution:

Select case when column > 'condition value' then 'value 1' else 'value 2' end from table

That is:

Select *, case when ssex=1 then 'male' else 'female' end as' gender 'from student

Tags: Gender Zhang San Li Si Li Xing Yang Jing condition effect scheme structure solution statement field plus one Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Huawei Docker Shulou Tech Info Shulou Information