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;
输出
时间: 2024-09-28 21:09:25