sqlÖÐwhenºÍcaseɶÒâ˼
sqlÖеÄwhenºÍcaseÊÇÓÃÓÚÌõ¼þ¼ì²éºÍ·µ»ØÖ¸¶¨ÖµµÄÒªº¦×Ö¡£whenÓÃÓÚÖ¸¶¨Ìõ¼þ£¬ÈôÊÇÌõ¼þΪÕ棬ÔòÖ´ÐÐËæºóµÄÓï¾ä£»caseÊÇ¿ØÖÆÁ÷Óï¾ä£¬Æ¾Ö¤Öª×ãµÄÌõ¼þÖ´Ðвî±ðµÄÓï¾ä£¬Ó﷨Ϊ£ºcase when then when then … else end¡£Ìõ¼þ¿ÉÒÔÊÇÈκÎÓÐÓõÄsql±í´ïʽ£¬Ê¾Àý£ºselect cas
SQL ÖÐµÄ WHEN ºÍ CASE
ʲôÊÇ WHEN ºÍ CASE£¿
WHEN ºÍ CASE ÊÇ SQL ÖÐÓÃÓÚÖ´ÐÐÌõ¼þ¼ì²éºÍ·µ»ØÖ¸¶¨ÖµµÄÒ»×éÒªº¦×Ö¡£
WHEN
WHEN ÊÇÒ»¸öÌõ¼þÐÔÒªº¦×Ö£¬ÓÃÓÚÔÚ CASE Óï¾äÖÐÖ¸¶¨Ìõ¼þ¡£
ÈôÊÇÌõ¼þΪÕ棬ÔòÖ´ÐÐËæºóµÄÓï¾ä¡£
CASE
CASE ÊÇÒ»¸ö¿ØÖÆÁ÷Óï¾ä£¬ÓÃÓÚƾ֤֪×ãµÄÌõ¼þÖ´Ðвî±ðµÄÓï¾ä¡£
CASE Óï¾äÓÉÒ»¸ö»ò¶à¸ö WHEN ×Ó¾äºÍÒ»¸ö¿ÉÑ¡µÄ ELSE ×Ó¾ä×é³É¡£
ÔõÑùʹÓà WHEN ºÍ CASE
CASE Óï¾äµÄÒ»Ñùƽ³£Óï·¨ÈçÏ£º
CASE WHEN <condition1> THEN <result1> WHEN <condition2> THEN <result2> ... ELSE <default_result> END</default_result></result2></condition2></result1></condition1>
µÇ¼ºó¸´ÖÆ
Ìõ¼þ¿ÉÒÔÊÇÈκÎÓÐÓÃµÄ SQL ±í´ïʽ£¬ÀýÈç½ÏÁ¿¡¢Âß¼ÔËËã»ò×ÓÅÌÎÊ¡£
ʾÀý
ÒÔÏÂʾÀýʹÓà CASE Óï¾ä·µ»Ø¿Í»§×´Ì¬£º
SELECT CASE WHEN status = 'A' THEN 'Active' WHEN status = 'I' THEN 'Inactive' ELSE 'Unknown' END AS customer_status FROM customers;
µÇ¼ºó¸´ÖÆ
ÆäËûÌáÐÑ
¿ÉÒÔʹÓöà¸ö WHEN ×Ó¾äÀ´´¦Öóͷ£¶à¸öÌõ¼þ¡£
ELSE ×Ó¾äÊÇ¿ÉÑ¡µÄ£¬ÓÃÓÚ´¦Öóͷ£²»Öª×ãÈκÎÆäËûÌõ¼þµÄÇéÐΡ£
CASE Óï¾äµÄЧ¹û¿ÉÒÔÊÇÈκÎÊý¾ÝÀàÐÍ¡£
WHEN ºÍ CASE ¿ÉÓÃÓÚ´¦Öóͷ£ÖØ´óÌõ¼þ²¢¶¯Ì¬ÌìÉúЧ¹û¡£
ÒÔÉϾÍÊÇsqlÖÐwhenºÍcaseɶÒâ˼µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡