sqlÖÐÌõ¼þÅжÏÓï¾äÓÐÄÄЩ
sqlÖеÄÌõ¼þÅжÏÓï¾äÓÃÓÚÈ·¶¨Ìõ¼þÕæ¼Ù£¬²¢Ö´ÐÐÏìÓ¦µÄ²Ù×÷¡£Ö÷ÒªÓÐÁ½ÖÖÀàÐÍ£ºif-else Óï¾ä£ºÆ¾Ö¤Ìõ¼þÕæ¼Ù£¬Ö´Ðвî±ðµÄÓï¾ä¡£case Óï¾ä£ºÆ¾Ö¤Ìõ¼þÆ¥Å䣬·µ»Ø²î±ðµÄЧ¹û¡£
SQLÖеÄÌõ¼þÅжÏÓï¾ä
SQL ÖÐʹÓÃÌõ¼þÅжÏÓï¾äÀ´È·¶¨Ìض¨Ìõ¼þÊÇ·ñΪÕæ»ò¼Ù£¬²¢ÏìÓ¦µØÖ´Ðвî±ðµÄ²Ù×÷¡£Ö÷ÒªÓÐÁ½ÖÖÀàÐ͵ÄÌõ¼þÅжÏÓï¾ä£º
1. IF-ELSE Óï¾ä
IF (condition) THEN statement1 ELSE statement2
µÇ¼ºó¸´ÖÆ
condition£ºÒªÆÀ¹ÀµÄÌõ¼þ¡£
statement1£ºÈôÊÇÌõ¼þΪÕ棬ÔòÖ´ÐеÄÓï¾ä¡£
statement2£ºÈôÊÇÌõ¼þΪ¼Ù£¬ÔòÖ´ÐеÄÓï¾ä¡£
2. CASE Óï¾ä
CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ... ELSE default_result END
µÇ¼ºó¸´ÖÆ
condition1, condition2, …£ºÒªÆÀ¹ÀµÄÌõ¼þ¡£
result1, result2, …£ºÈôÊÇÏìÓ¦Ìõ¼þΪÕ棬Ôò·µ»ØµÄЧ¹û¡£
default_result£ºÈôÊÇËùÓÐÌõ¼þ¶¼Îª¼Ù£¬Ôò·µ»ØµÄĬÈÏЧ¹û£¨¿ÉÑ¡£©¡£
ʾÀý
IF-ELSE Óï¾ä
IF (age > 18) THEN PRINT "³ÉÄê" ELSE PRINT "δ³ÉÄê"
µÇ¼ºó¸´ÖÆ
CASE Óï¾ä
CASE WHEN gender = 'M' THEN "ÄÐÐÔ" WHEN gender = 'F' THEN "Å®ÐÔ" ELSE "ÆäËû" END
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇsqlÖÐÌõ¼þÅжÏÓï¾äÓÐÄÄЩµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡