简体   繁体   中英

Conditionally set value of column in MySQL select query

I have a simple query in MySQL

select AGE, NAME from Members;

which returns,

20 ABC
11 PQR
21 XYZ
16 REW

I need to conditionally select NAME from Members, ie if AGE is less then 18 I need to print MINOR. So my output will be

20 ABC
11 MINOR
21 XYZ
16 MINOR

I know I should do this programmatically, but I am dumping the output directly to a file in CSV format using

 INTO OUTFILE '$random_file_name' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n'

What are my options in MySQL query itself to achieve this?

select AGE,
       case when AGE < 18 
            then 'MINOR' 
            else NAME 
       end as NAME
from Members

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM