1. In the WHERE clause:
1 create TABLE ' hello '. ' Sometbl ' ( Span style= "color: #008080;" > 2 3 ' id ' INT not NULL auto_increment, 5 6 7 ' B ' VARCHAR NULL, 8 9 ' C ' VARCHAR 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');
There are two records in query a,b,c that meet the =1 criteria
SELECT * fromHello.sometblwhere(( Case whenA='1' Then 1 Else 0 End)+( Case whenB='1' Then 1 Else 0 End)+( Case whenC='1' Then 1 Else 0 End))=2;
2. In select as a column
Output when ID 1,2,3,4, one, two, three, four
SELECTId Case whenId=1 Then 'a' whenId=2 Then 'two' whenId=3 Then 'three' whenId=4 Then 'Four'Else 'other' EndChinese numerals fromHELLO.SOMETBL;
Output
Case-when statement in SQL