Use in sql (case when then else end) (corresponding to the java if else)

 

Case has two formats: Simple Case Case function and search function.

1. Simple Case function:

CASE sex

WHEN‘1’THEN‘男’

WHEN‘0’THEN‘女’

ELSE 'Other' END

2.Case search function:

CASE WHEN sex=‘1’THEN‘男’

WHEN sex=‘0’THEN‘女’

ELSE 'Other' END

Clearly, the simple raw Case function in simple, but it only applies to the comparison of such single-field single value, while the search function Case advantage that the comparison for all cases.

Note that there is a problem, Case function after a qualifying meet the remaining conditions will be automatically ignored, so even if a number of conditions are met, the implementation process only recognize the first condition.

In use CASE WHEN, it can not as a field name field, the field values ​​according to the condition confirmation, when required for use with the field names may be defined as an alias.

Guess you like

Origin www.cnblogs.com/mark5/p/11331494.html