前提・実現したいこと
SQLでSELECTで名前を変更した値を検索したい。
該当のソースコード
SQL
1SELECT 2 CASE 3 WHEN gender = '女' THEN 0 4 WHEN gender = '男' THEN 1 5 WHEN gender = null THEN 2 6 ELSE 99 7 END as gender1 8 FROM users where gender1 = 2 9 10SELECT 11 IF( 12 gender = null, gender = 2, 13 gender 14) gender1 15FROM users 16where gender1 = 2; 17 18 19create table users 20( 21 id int auto_increment 22 primary key, 23 gender varchar(10) null 24); 25 26 27INSERT INTO users (gender) VALUES ('男'); 28INSERT INTO users (gender) VALUES ('男'); 29INSERT INTO users (gender) VALUES ('男'); 30INSERT INTO users (gender) VALUES ('男'); 31INSERT INTO users (gender) VALUES ('男'); 32INSERT INTO users (gender) VALUES ('女'); 33INSERT INTO users (gender) VALUES ('女'); 34INSERT INTO users (gender) VALUES ('女'); 35INSERT INTO users (gender) VALUES ('女'); 36INSERT INTO users (gender) VALUES ('女'); 37INSERT INTO users (gender) VALUES (null); 38INSERT INTO users (gender) VALUES (null); 39INSERT INTO users (gender) VALUES (null); 40INSERT INTO users (gender) VALUES (null); 41INSERT INTO users (gender) VALUES (null);
期待値
id | gender |
---|---|
11 | null |
12 | null |
13 | null |
14 | null |
15 | null |
ご教授ください。
追記
SELECT gender as g1 FROM users where g1 is null;