Case
When condition 1 then result 1
When condition 2 then result 2
When condition 3 then result 3
When condition 4 then result 4
.........
When condition n then result n
ELSE result X
END
Case has two formats. Simple case function and case search function.
--Simple Case function
Case Sex
When ' 1 ' Then ' men '
When ' 2 ' then ' women '
Else ' other ' END
--case search function
case If sex = ' 1 ' Then ' man '
When sex = ' 2 ' Then ' women '
Else ' other ' END
For example:
SELECT ID, name, CJ, (case at CJ < Then ' fail ' when CJ between and all then ' good ' when CJ > all then ' Excellent ' END) A S State
From stud
In both of these ways, the same functionality can be achieved. The simple case function is relatively concise, but there are some limitations in function, such as write-judgement, compared to the search function.
There is also a problem to be aware that the case function returns only the first qualifying value, and the remaining case section is automatically ignored.
--for example, the following SQL, you can never get the result of "type two"
Case is col_1 in (' A ', ' B ') then ' first Class '
When Col_1 in (' a ') then ' second class '
Else ' other ' END
Such as:
Case-Created >[email protected] and Created<[email protected] then 1 else 0 end
Real-combat basic Skills (-------) SQL using case and then multi-criteria judgment