sqlÖÐisnullµÄÓ÷¨
sql ÖÐ isnull º¯Êý¼ì²éÖµÊÇ·ñΪ null£¬²¢·µ»ØÖ¸¶¨Öµ£¨ÈôÊÇֵΪ null£©»òÔʼֵ£¨ÈôÊÇֵΪ·Ç null£©¡£ËüµÄÓ﷨Ϊ£ºisnull(expression, replacement_value)¡£¸Ãº¯ÊýÓÃÓÚÌæ»» null ÖµÒÔÌá¸ß¿É¶ÁÐÔ¡¢±ÜÃâ¹ýʧºÍÔھۺϺ¯ÊýÖд¦Àí null Öµ¡£
SQL ÖÐ ISNULL º¯ÊýµÄÓ÷¨
ʲôÊÇ ISNULL º¯Êý£¿
ISNULL º¯ÊýÊÇÒ»ÖÖ SQL º¯Êý£¬ÓÃÓÚ¼ì²éÖµÊÇ·ñΪ NULL£¬²¢·µ»ØÒ»¸öÖ¸¶¨Öµ£¨ÈôÊÇֵΪ NULL£©»òÔʼֵ£¨ÈôÊÇֵΪ·Ç NULL£©¡£
Óï·¨£º
ISNULL(expression, replacement_value)
ÆäÖУº
expression£ºÒª¼ì²éµÄÖµ¡£
replacement_value£ºÈôÊÇ expression Ϊ NULL£¬Ôò·µ»ØµÄÖµ¡£
Ó÷¨£º
ISNULL º¯Êý¿ÉÓÃÓÚÖÖÖÖÇéÐΣ¬ÀýÈ磺
Ìæ»» NULL ֵΪ¸üÓѺõÄÖµ£¬ÒÔÌá¸ßÊý¾Ý¿É¶ÁÐÔ¡£
±ÜÃâ¿ÕÖµµ¼Ö¹ýʧ»òÒâÍâЧ¹û¡£
ÔھۺϺ¯Êý£¨Èç SUM¡¢COUNT£©Öд¦Àí NULL Öµ¡£
ʾÀý£º
˼Á¿ÒÔϱí¸ñ£º
ID | Name |
---|---|
1 | John Smith |
2 | NULL |
ÒªÌæ»» Name ÁÐÖÐΪ NULL µÄÖµ£¬¿ÉÒÔʹÓÃÒÔÏÂÅÌÎÊ£º
SELECT ID, ISNULL(Name, 'Unknown') FROM TableName;
µÇ¼ºó¸´ÖÆ
Êä³ö£º
ID | Name |
---|---|
1 | John Smith |
2 | Unknown |
×¢ÖØÊÂÏ
ISNULL º¯ÊýµÄµÚÒ»¸ö²ÎÊý±ØÐèÊDZí´ïʽ£¬²»¿ÉÊÇ×ÓÅÌÎÊ¡£
ÈôÊÇ replacement_value Ϊ NULL£¬ISNULL º¯Êý½«·µ»Ø expression ×Ô¼º£¬¶ø²»»á¾ÙÐÐÌæ»»¡£
ISNULL º¯ÊýÓë COALESCE º¯ÊýÀàËÆ£¬µ« COALESCE º¯Êý¿ÉÒÔ½ÓÊܶà¸ö replacement_value¡£
ÒÔÉϾÍÊÇsqlÖÐisnullµÄÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡