sqlÖеÄdecodeÊÇʲôÒâ˼
decode º¯ÊýÓÃÓÚƾ֤Ìõ¼þ·µ»Ø²î±ðÖµ£¬½«Öµ´ÓÒ»ÖÖÃûÌÃת»»ÎªÁíÒ»ÖÖÃûÌá£Óï·¨£ºdecode(expression, search1, result1, search2, result2, …, default)¡£Ëü½«Öð¸ö¼ì²é search Öµ²¢·µ»ØÏìÓ¦µÄ result Öµ£¬ÈôÊÇÎÞÆ¥ÅäÔò·µ»Ø default Öµ¡£
SQL ÖÐµÄ DECODE
DECODE ÊÇ SQL ÖÐÒ»¸öº¯Êý£¬ÓÃÓÚƾָ֤¶¨Ìõ¼þ·µ»Ø²î±ðµÄÖµ¡£ËüÊÇÒ»ÖÖ½«Öµ´ÓÒ»ÖÖÃûÌÃת»»ÎªÁíÒ»ÖÖÃûÌõÄÇ¿Ê¢¹¤¾ß¡£
Óï·¨
DECODE(expression, search1, result1, search2, result2, ..., default)
µÇ¼ºó¸´ÖÆ
ÆäÖУº
expression£º´ýÆÀ¹ÀµÄ±í´ïʽ¡£
search1, search2, …£ºÒªÆ¥Åä expression µÄһϵÁÐÖµ¡£
result1, result2, …£ºÈôÊÇ expression ÓëÏìÓ¦µÄ search ֵƥÅ䣬Ôò·µ»ØµÄÖµ¡£
default£ºÈôÊÇ expression ÓëÈκΠsearch Öµ¶¼²»Æ¥Å䣬Ôò·µ»ØµÄĬÈÏÖµ¡£
ÊÂÇéÔÀí
DECODE º¯ÊýÖð¸ö¼ì²é search Öµ£¬ÈôÊÇ expression Óë search ֵƥÅ䣬Ôò·µ»ØÏìÓ¦µÄ result Öµ¡£ÈôÊÇ expression ÓëËùÓÐ search Öµ¶¼²»Æ¥Å䣬Ôò·µ»Ø default Öµ¡£
ʾÀý
¼ÙÉèÎÒÃÇÓÐÒ»¸öÃûΪ status µÄÁУ¬ÆäÖаüÀ¨Öµ “A”¡¢”B” ºÍ “C”¡£ÎÒÃÇÏ£Íûƾ֤״ֵ̬½«ÕâЩֵת»»ÎªÎı¾ÐÎò¡£ÎÒÃÇ¿ÉÒÔʹÓà DECODE º¯ÊýÈçÏ£º
SELECT DECODE(status, 'A', 'Active', 'B', 'Inactive', 'C', 'Pending', 'Unknown') FROM table_name;
µÇ¼ºó¸´ÖÆ
Õ⽫·µ»ØÒÔÏÂЧ¹û£º
status | description | |
---|---|---|
A | Active | |
B | Inactive | |
C | Pending | |
NULL | Unknown | (default Öµ) |
Óŵã
ʹÓà DECODE º¯ÊýµÄÖ÷ÒªÓŵã°üÀ¨£º
Àû±ãµØ½«Öµ´ÓÒ»ÖÖÃûÌÃת»»ÎªÁíÒ»ÖÖÃûÌá£
Ìṩ¾«Á·µÄ·½·¨À´´¦ÀíÖØ´óµÄÌõ¼þÂß¼¡£
Ìá¸ß´úÂëµÄ¿É¶ÁÐԺͿÉά»¤ÐÔ¡£
ÒÔÉϾÍÊÇsqlÖеÄdecodeÊÇʲôÒâ˼µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡