sqlÖÐif elseµÄÓ÷¨
if else Óï¾äÔÊÐíÔÚ sql ÅÌÎÊÖлùÓÚÌõ¼þÖ´Ðвî±ð²Ù×÷¡£ËüÓÃÓÚÌõ¼þ¼ì²é£¬Æ¾Ö¤Ð§¹ûÖ´ÐÐ then »ò else ÖÐÖ¸¶¨µÄÓï¾ä¡£Ó﷨Ϊ£ºif Ìõ¼þ then ²Ù×÷1 else ²Ù×÷2 end if¡£Ê¾Àý£ºselect product, if(product = ‘book’, ‘Êé¼®’, ‘ÔÓÖ¾’) as producttype from sales; ƾ֤ product ÁеÄÖµ£¬ÏÔʾ “Êé¼®” »ò “ÔÓÖ¾” ÐÂÎÅ¡£
SQL ÖÐ IF ELSE Óï¾äµÄÓ÷¨
IF ELSE Óï¾äÔÊÐíÄúÔÚ SQL ÅÌÎÊÖÐÖ´ÐÐÌõ¼þ¼ì²é£¬²¢Æ¾Ö¤Ð§¹ûÖ´Ðвî±ðµÄ²Ù×÷¡£
Óï·¨£º
IF Ìõ¼þ THEN ²Ù×÷1 ELSE ²Ù×÷2 END IF;
µÇ¼ºó¸´ÖÆ
ʾÀý£º
¼ÙÉèÄúÓÐÒ»¸öÃûΪ “Sales” µÄ±í£¬ÆäÖаüÀ¨Ã¿¸ö¶©µ¥µÄ “Product” ºÍ “Amount” ÁС£Òª¼ì²éÿ¸ö¶©µ¥ÖвúÆ·ÀàÐÍÊÇ “Book” ÕÕ¾É “Magazine”£¬²¢ÏìÓ¦µØÏÔʾ²î±ðµÄÐÂÎÅ£¬¿ÉÒÔʹÓÃÒÔÏ IF ELSE Óï¾ä£º
SELECT Product, IF(Product = 'Book', 'ÕâÊǶ©¹ºµÄÊé¼®', 'ÕâÊǶ©¹ºµÄÔÓÖ¾') AS ProductType FROM Sales;
µÇ¼ºó¸´ÖÆ
Ú¹ÊÍ£º
Ìõ¼þ¼ì²éÔÚÔ²À¨ºÅÖÐÖ¸¶¨£ºProduct = ‘Book’¡£
ÈôÊÇÌõ¼þΪ true£¨²úÆ·ÊÇ “Book”£©£¬ÔòÖ´ÐÐ THEN ²Ù×÷£ºÏÔʾ “ÕâÊǶ©¹ºµÄÊé¼®”¡£
ÈôÊÇÌõ¼þΪ false£¨²úÆ·²»ÊÇ “Book”£©£¬ÔòÖ´ÐÐ ELSE ²Ù×÷£ºÏÔʾ “ÕâÊǶ©¹ºµÄÔÓÖ¾”¡£
×¢ÖØÊÂÏ
IF ELSE Óï¾ä¿ÉÒÔǶÌ×£¬ÒÔ´¦Àí¸üÖØ´óµÄÌõ¼þ¡£
ELSE ×Ó¾äÊÇ¿ÉÑ¡µÄ£¬ÈôÊÇÄúûÓÐÖ¸¶¨ ELSE ×Ӿ䣬Ôòµ±Ìõ¼þΪ false ʱ²»»áÖ´ÐÐÈκβÙ×÷¡£
Ìõ¼þ¼ì²éµÄЧ¹û±ØÐèΪ²¼¶ûÖµ£¨true »ò false£©¡£
IF ELSE Óï¾äÖеIJÙ×÷¿ÉÒÔÊÇÈκÎÓÐÓÃµÄ SQL Óï¾ä£¬°üÀ¨ SELECT¡¢INSERT¡¢UPDATE ºÍ DELETE¡£
ÒÔÉϾÍÊÇsqlÖÐif elseµÄÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡