sqlÖÐifnullµÄÓ÷¨
ifnull º¯ÊýÓÃÓÚ¼ì²é±í´ïʽÊÇ·ñΪ null£¬ÈôÊÇÊÇÔò·µ»ØÖ¸¶¨µÄĬÈÏÖµ£¬²»È»·µ»Ø±í´ïʽµÄÖµ¡£Ëü¿ÉÒÔ±ÜÃâ¿ÕÖµµ¼Ö¹ýʧ£¬ÔÊÐí²Ù×÷¿ÕÖµ£¬Ìá¸ßÅÌÎʵĿɶÁÐÔ¡£Ó÷¨°üÀ¨£º½«¿ÕÖµÌ滻ΪĬÈÏÖµ¡¢É¨³ý¿ÕÖµ¾ÙÐÐÅÌËãºÍǶÌ×ʹÓô¦Àí¶à¸ö¿ÕÖµÇéÐΡ£
SQL ÖÐ IFNULL µÄÓ÷¨
IFNULL º¯ÊýÓÃÓÚ¼ì²é±í´ïʽÊÇ·ñΪ NULL£¬ÈôÊÇΪ NULL£¬Ôò·µ»ØÖ¸¶¨µÄĬÈÏÖµ£»ÈôÊDz»ÊÇ NULL£¬Ôò·µ»Ø±í´ïʽµÄÖµ¡£
Óï·¨£º
IFNULL(expression, default_value)
µÇ¼ºó¸´ÖÆ
ÆäÖУº
expression£ºÒª¼ì²éµÄ±í´ïʽ¡£
default_value£ºÈôÊÇ expression Ϊ NULL ʱҪ·µ»ØµÄÖµ¡£
Ó÷¨£º
IFNULL º¯ÊýÖ÷ÒªÓÃÓÚ½«¿ÕÖµÌ滻Ϊָ¶¨µÄĬÈÏÖµ¡£ÀýÈ磬ÒÔÏÂÅÌÎÊʹÓà IFNULL ½« name ÁÐÖеĿÕÖµÌ滻Ϊ×Ö·û´® “ÎÞÃû”£º
SELECT IFNULL(name, 'ÎÞÃû') AS name FROM table_name;
µÇ¼ºó¸´ÖÆ
Óŵ㣺
±ÜÃâÓÉÓÚ¿ÕÖµ¶øµ¼Ö¹ýʧ»òÒâÍâЧ¹û¡£
ÔÊÐíÔÚ¿ÕÖµÉÏÖ´ÐÐÅÌËã»ò²Ù×÷¡£
Ìá¸ßÅÌÎʵĿɶÁÐԺͿÉά»¤ÐÔ¡£
×¢ÖØÊÂÏ
default_value ¿ÉÒÔÊÇÈκÎÊý¾ÝÀàÐÍ¡£
ÈôÊÇ expression ºÍ default_value µÄÊý¾ÝÀàÐͲ»Æ¥Å䣬Ôò SQL ÒýÇ潫×Ô¶¯¾ÙÐÐÒþʽת»»¡£
IFNULL º¯ÊýÒ²¿ÉÒÔǶÌ×ʹÓã¬ÒÔ´¦Àí¶à¸ö¿ÕÖµÇéÐΡ£
ʾÀý£º
½« age ÁÐÖеĿÕÖµÌ滻Ϊ 0£º
SELECT IFNULL(age, 0) AS age FROM table_name;
µÇ¼ºó¸´ÖÆ
ÔÚ COUNT() º¯ÊýÖÐɨ³ý¿ÕÖµ£º
SELECT COUNT(IFNULL(name, '')) AS num_names FROM table_name;
µÇ¼ºó¸´ÖÆ
ʹÓÃǶÌ×µÄ IFNULL º¯Êý´¦Àí¶à¸ö¿ÕÖµ£º
SELECT IFNULL(name, IFNULL(nickname, 'ÎÞÃû')) AS name FROM table_name;
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇsqlÖÐifnullµÄÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡