1.在where子句中:
1 CREATE TABLE `hello`.`sometbl` ( 2 3 `id` INT NOT NULL AUTO_INCREMENT , 4 5 `a` VARCHAR(45) NULL , 6 7 `b` VARCHAR(45) NULL , 8 9 `c` VARCHAR(45) NULL , 10 11 PRIMARY KEY (`id`) , 12 13 UNIQUE INDEX `id_UNIQUE` (`id` ASC) );
1 INSERT INTO `hello`.`sometbl` (`a`, `b`, `c`) VALUES ('1', '1', '0'); 2 3 INSERT INTO `hello`.`sometbl` (`b`, `c`) VALUES ('1', '1'); 4 5 INSERT INTO `hello`.`sometbl` (`a`, `c`) VALUES ('1', '1'); 6 7 INSERT INTO `hello`.`sometbl` (`b`) VALUES ('1');
查詢a,b,c中有兩個滿足=1條件的記錄
SELECT * FROM hello.sometbl where ((case when a='1' then 1 else 0 end) +(case when b='1' then 1 else 0 end) +(case when c='1' then 1 else 0 end))=2;
2。在select中作為一列
id為1,2,3,4時分別輸出,一,二,三,四
SELECT id,case when id=1 then '一' when id=2 then '二' when id=3 then '三' when id=4 then '四' else '其它' end 中文數字 FROM hello.sometbl;
輸出