sqlÖÐwhenÓ÷¨
sql ÖÐ when ×Ó¾äÓÃÓÚÔÚ case ±í´ïʽÖÐÖ¸¶¨Ìõ¼þ²¢·µ»ØÏìÓ¦Êä³ö¡£Óï·¨ÈçÏ£ºcase when condition then result end¡£Ìõ¼þΪ true ʱ£¬·µ»Ø¶ÔÓ¦µÄЧ¹û¡£
SQL ÖÐ WHEN Ó÷¨
ʲôÊÇ WHEN ×Ӿ䣿
WHEN ×Ó¾äÓÃÓÚÔÚ CASE ±í´ïʽÖÐÖ¸¶¨Ìõ¼þ²¢·µ»ØÏìÓ¦µÄÊä³ö¡£
Óï·¨£º
CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ... ELSE result_default -- ¿ÉÑ¡ END
µÇ¼ºó¸´ÖÆ
ÔõÑùʹÓà WHEN ×Ӿ䣿
Ö¸¶¨Ìõ¼þ£ºcondition1¡¢condition2 µÈÌåÏÖÒªÆÀ¹ÀµÄÌõ¼þ¡£Ð§¹ûΪ TRUE ʱ£¬½«Ö´ÐжÔÓ¦µÄÊä³ö¡£
·µ»ØÊä³ö£ºresult1¡¢result2 µÈÊÇÖª×ãÌõ¼þºó·µ»ØµÄÖµ¡£
ĬÈÏÊä³ö£¨¿ÉÑ¡£©£ºELSE ×Ó¾äÖ¸¶¨µ±ËùÓÐÌõ¼þ¶¼²»Öª×ãʱµÄĬÈÏÊä³öÖµ¡£ÈôÊÇδָ¶¨ ELSE£¬Ôò·µ»Ø NULL¡£
ʾÀý£º
-- ƾ֤Ч¹ûÅÌËãÆ·¼¶ CASE WHEN grade >= 90 THEN 'ÓŵÈ' WHEN grade >= 80 THEN 'ÓÅÒì' WHEN grade >= 70 THEN 'ÖеÈ' ELSE 'ȱ·¦¸ñ' END
µÇ¼ºó¸´ÖÆ
×¢ÖØÊÂÏ
WHEN ×Ӿ䰴˳ÐòÆÀ¹À¡£Ò»µ©Öª×ãij¸öÌõ¼þ£¬½«×èÖ¹ÆÀ¹À²¢·µ»ØÏìÓ¦µÄÊä³ö¡£
CASE ±í´ïʽ¿ÉÒÔǶÌס£
WHEN ×Ó¾ä¿ÉÓÃÓÚÅÌÎÊ¡¢¸üкͲåÈëÓï¾äÖС£
ÒÔÉϾÍÊÇsqlÖÐwhenÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡