sqlÖÐcaseÊÇʲôÒâ˼
case Óï¾äÊÇÒ»¸ö sql ¿ØÖÆÁ÷Óï¾ä£¬Ëüƾ֤Ìض¨Ìõ¼þÖ´Ðвî±ðµÄ²Ù×÷¡£ÆäÓ﷨Ϊ£ºcase when then when then … else end¡£Ëüͨ¹ý¼ì²é¸ø¶¨±í´ïʽÀ´Ñ¡Ôñ²î±ðµÄ´úÂë¿é£¬²¢Æ¾Ö¤ÐèÒª·ÖÅÉЧ¹û¡¢ÉèÖñê¼Ç²¢Ö´ÐÐÖØ´óÅÌÎÊ¡£case Óï¾äÓмòÆÓ case Óï¾ä¡¢ËÑË÷ case Óï¾äºÍÖµ ca
SQL ÖÐµÄ CASE Óï¾ä
ʲôÊÇ CASE Óï¾ä£¿
CASE Óï¾äÊÇÒ»ÖÖ SQL ¿ØÖÆÁ÷Óï¾ä£¬ÓÃÓÚƾ֤Ìض¨Ìõ¼þÖ´Ðвî±ðµÄ²Ù×÷¡£ËüÔÊÐíÄú»ùÓÚij¸ö±í´ïʽµÄÖµÀ´Ñ¡Ôñ²î±ðµÄ´úÂë¿é¡£
CASE Óï¾äµÄÓï·¨
CASE WHEN <expression1> THEN <result1> WHEN <expression2> THEN <result2> ... ELSE <default_result> END</default_result></result2></expression2></result1></expression1>
µÇ¼ºó¸´ÖÆ
CASE Óï¾äµÄÊÂÇéÔÀí
CASE Óï¾äÆÀ¹À¸ø¶¨µÄ±í´ïʽ²¢°´Ë³Ðò¼ì²é WHEN ×Ӿ䡣ÈôÊÇij¸ö WHEN ×Ó¾äµÄ±í´ïʽΪÕ棬ÔòÖ´ÐÐÏìÓ¦µÄ RESULT ×Ӿ䡣ÈôÊÇËùÓÐ WHEN ×Ó¾äµÄ±í´ïʽ¶¼Îª¼Ù£¬ÔòÖ´ÐÐ ELSE ×Ӿ䣨ÈôÊDZ£´æ£©¡£
ºÎʱʹÓà CASE Óï¾ä
CASE Óï¾äͨ³£ÓÃÓÚÒÔÏÂÇéÐΣº
ƾ֤Ìõ¼þÑ¡Ôñ²î±ðµÄÁÐÖµ
»ùÓÚ²î±ðµÄÖµ¹æÄ£ÉèÖòî±ðµÄ±ê¼Ç
Ö´ÐÐÖØ´óµÄÌõ¼þÅÌÎÊ
ʾÀý
ÒÔÏÂÊÇÒ»¸öʹÓà CASE Óï¾äƾ֤·ÖÊý¹æÄ£·ÖÅÉЧ¹ûµÄʾÀý£º
SELECT CASE WHEN score >= 90 THEN 'A' WHEN score >= 80 THEN 'B' WHEN score >= 70 THEN 'C' ELSE 'F' END AS grade FROM students;
µÇ¼ºó¸´ÖÆ
ÆäËûÀàÐÍ
³ýÁËÉÏÊöÓï·¨Í⣬CASE Óï¾äÉÐÓÐÆäËûÀàÐÍ£¬°üÀ¨£º
¼òÆÓ CASE Óï¾ä£º½öÓÐÒ»¸ö CASE Òªº¦×ÖºÍÒ»¸ö THEN ×Ӿ䡣
ËÑË÷ CASE Óï¾ä£ºÊ¹Óà WHEN THEN ×Ó¾äÐòÁÐÀ´ËÑË÷Ìض¨Öµ¡£
Öµ CASE Óï¾ä£ºÊ¹Óà VALUE WHEN ×Ó¾äÐòÁÐÀ´Æ¥ÅäÁÐÖµ¡£
ͨ¹ýʹÓòî±ðµÄÀàÐÍ£¬Äú¿ÉÒÔƾ֤ÐèÒª¶¨ÖÆ CASE Óï¾äµÄÐÐΪ¡£
ÒÔÉϾÍÊÇsqlÖÐcaseÊÇʲôÒâ˼µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡