×ðÁú¿­Ê±¹ÙÍøµÇ¼

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 &gt;= 90 THEN 'A'
        WHEN score &gt;= 80 THEN 'B'
        WHEN score &gt;= 70 THEN 'C'
        ELSE 'F'
    END AS grade
FROM students;

µÇ¼ºó¸´ÖÆ

ÆäËûÀàÐÍ

³ýÁËÉÏÊöÓï·¨Í⣬CASE Óï¾äÉÐÓÐÆäËûÀàÐÍ£¬°üÀ¨£º

¼òÆÓ CASE Óï¾ä£º½öÓÐÒ»¸ö CASE Òªº¦×ÖºÍÒ»¸ö THEN ×Ӿ䡣

ËÑË÷ CASE Óï¾ä£ºÊ¹Óà WHEN THEN ×Ó¾äÐòÁÐÀ´ËÑË÷Ìض¨Öµ¡£

Öµ CASE Óï¾ä£ºÊ¹Óà VALUE WHEN ×Ó¾äÐòÁÐÀ´Æ¥ÅäÁÐÖµ¡£

ͨ¹ýʹÓòî±ðµÄÀàÐÍ£¬Äú¿ÉÒÔƾ֤ÐèÒª¶¨ÖÆ CASE Óï¾äµÄÐÐΪ¡£

ÒÔÉϾÍÊÇsqlÖÐcaseÊÇʲôÒâ˼µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ