{"id":6414,"date":"2024-11-14T08:01:39","date_gmt":"2024-11-14T00:01:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6414\/"},"modified":"2024-11-14T08:01:39","modified_gmt":"2024-11-14T00:01:39","slug":"mysql-ifnull%e4%b8%8d%e8%b5%b7%e4%bd%9c%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6414\/","title":{"rendered":"mysql ifnull\u4e0d\u8d77\u4f5c\u7528"},"content":{"rendered":"<blockquote><p>\n  \u5f53 mysql ifnull() \u51fd\u6570\u4e0d\u8d77\u4f5c\u7528\u65f6\uff0c\u53ef\u80fd\u662f\u7531\u4e8e\u4ee5\u4e0b\u539f\u56e0\uff1a1. mysql \u7248\u672c\u8fc7\u4f4e\uff1b2. \u8bed\u6cd5\u9519\u8bef\uff1b3. \u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d\uff1b4. \u7a7a\u503c\u4e0d\u662f null\uff1b5. null \u503c\u5d4c\u5957\u3002\u89e3\u51b3\u65b9\u6cd5\u5305\u62ec\u5347\u7ea7 mysql \u7248\u672c\u3001\u68c0\u67e5\u8bed\u6cd5\u3001\u5339\u914d\u6570\u636e\u7c7b\u578b\u3001\u68c0\u67e5\u7a7a\u503c\u548c\u91cd\u65b0\u7f16\u5199\u67e5\u8be2\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/02\/2024080207335114854.jpg\" class=\"aligncenter\" title=\"mysql ifnull\u4e0d\u8d77\u4f5c\u7528\u63d2\u56fe\" alt=\"mysql ifnull\u4e0d\u8d77\u4f5c\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u4e2d IFNULL \u4e3a\u4f55\u4e0d\u8d77\u4f5c\u7528\uff1f<\/strong><\/p>\n<p>\u5f53 IFNULL() \u51fd\u6570\u5728 MySQL \u4e2d\u4e0d\u8d77\u4f5c\u7528\u65f6\uff0c\u901a\u5e38\u6709\u4ee5\u4e0b\u539f\u56e0\uff1a<\/p>\n<p><strong>1. MySQL \u7248\u672c\u8fc7\u4f4e<\/strong><\/p>\n<p>IFNULL() \u51fd\u6570\u5728 MySQL 8.0 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\u624d\u88ab\u5f15\u5165\u3002\u5982\u679c\u4f7f\u7528\u7684\u662f\u8f83\u4f4e\u7248\u672c\u7684 MySQL\uff0c\u5219\u9700\u8981\u5347\u7ea7\u5230\u652f\u6301\u8be5\u51fd\u6570\u7684\u7248\u672c\u3002<\/p>\n<p><strong>2. \u8bed\u6cd5\u9519\u8bef<\/strong><\/p>\n<p>\u786e\u4fdd IFNULL() \u51fd\u6570\u7684\u8bed\u6cd5\u6b63\u786e\u3002\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<pre>IFNULL(expression1, expression2)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0cexpression1 \u662f\u8981\u68c0\u67e5\u7684\u503c\uff0cexpression2 \u662f\u5982\u679c expression1 \u4e3a NULL \u65f6\u8981\u8fd4\u56de\u7684\u503c\u3002<\/p>\n<p><strong>3. \u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d<\/strong><\/p>\n<p>expression1 \u548c expression2 \u7684\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u3002\u4f8b\u5982\uff0c\u5982\u679c expression1 \u4e3a\u6574\u6570\uff0c\u5219 expression2 \u4e5f\u5fc5\u987b\u4e3a\u6574\u6570\u3002<\/p>\n<p><strong>4. \u7a7a\u503c\u4e0d\u662f NULL<\/strong><\/p>\n<p>IFNULL() \u51fd\u6570\u4ec5\u5904\u7406 NULL \u503c\u3002\u5982\u679c expression1 \u4e3a\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32 (&#8221;)\u30010 \u6216\u5176\u4ed6\u975e NULL \u7a7a\u503c\uff0c\u5219\u8be5\u51fd\u6570\u5c06\u8fd4\u56de expression1\uff0c\u800c\u4e0d\u662f expression2\u3002<\/p>\n<p><strong>5. NULL \u503c\u5d4c\u5957<\/strong><\/p>\n<p>\u5982\u679c expression1 \u5305\u542b\u5d4c\u5957\u7684 NULL \u503c\uff0c\u5219 IFNULL() \u51fd\u6570\u53ef\u80fd\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>IFNULL(NULL, IFNULL(NULL, 'foo'))<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5c06\u8fd4\u56de NULL\uff0c\u800c\u4e0d\u662f &#8216;foo&#8217;\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u8981\u89e3\u51b3 IFNULL() \u51fd\u6570\u4e0d\u8d77\u4f5c\u7528\u7684\u95ee\u9898\uff0c\u8bf7\u5c1d\u8bd5\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u5347\u7ea7\u5230\u652f\u6301 IFNULL() \u51fd\u6570\u7684 MySQL \u7248\u672c\u3002<\/li>\n<li>\u68c0\u67e5\u51fd\u6570\u7684\u8bed\u6cd5\u662f\u5426\u6b63\u786e\u3002<\/li>\n<li>\u786e\u4fdd expression1 \u548c expression2 \u7684\u6570\u636e\u7c7b\u578b\u5339\u914d\u3002<\/li>\n<li>\u68c0\u67e5 expression1 \u662f\u5426\u5305\u542b\u975e NULL \u7a7a\u503c\u3002<\/li>\n<li>\u91cd\u65b0\u7f16\u5199\u67e5\u8be2\u4ee5\u907f\u514d\u5d4c\u5957 NULL \u503c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f ifnull\u4e0d\u8d77\u4f5c\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53 mysql ifnull() \u51fd\u6570\u4e0d\u8d77\u4f5c\u7528\u65f6\uff0c\u53ef\u80fd\u662f\u7531\u4e8e\u4ee5\u4e0b\u539f\u56e0\uff1a1. mysql \u7248\u672c\u8fc7\u4f4e\uff1b2. \u8bed\u6cd5\u9519\u8bef\uff1b3. \u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d\uff1b4. \u7a7a\u503c\u4e0d\u662f null\uff1b5. null \u503c\u5d4c\u5957\u3002\u89e3\u51b3\u65b9\u6cd5\u5305\u62ec\u5347\u7ea7 mysql \u7248\u672c\u3001\u68c0\u67e5\u8bed\u6cd5\u3001\u5339\u914d\u6570\u636e\u7c7b\u578b\u3001\u68c0\u67e5\u7a7a\u503c\u548c\u91cd\u65b0\u7f16\u5199\u67e5\u8be2\u3002 MySQL \u4e2d IFNULL \u4e3a\u4f55\u4e0d\u8d77\u4f5c\u7528\uff1f \u5f53 IFNULL() \u51fd\u6570\u5728 MySQL \u4e2d\u4e0d\u8d77\u4f5c\u7528\u65f6\uff0c\u901a\u5e38\u6709\u4ee5\u4e0b\u539f\u56e0\uff1a 1. MySQL \u7248\u672c\u8fc7\u4f4e IFNULL() \u51fd\u6570\u5728 MySQL 8.0 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\u624d\u88ab\u5f15\u5165\u3002\u5982\u679c\u4f7f\u7528\u7684\u662f\u8f83\u4f4e\u7248\u672c\u7684 MySQL\uff0c\u5219\u9700\u8981\u5347\u7ea7\u5230\u652f\u6301\u8be5\u51fd\u6570\u7684\u7248\u672c\u3002 2. \u8bed\u6cd5\u9519\u8bef \u786e\u4fdd IFNULL() \u51fd\u6570\u7684\u8bed\u6cd5\u6b63\u786e\u3002\u8bed\u6cd5\u4e3a\uff1a IFNULL(expression1, expression2) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0cexpression1 \u662f\u8981\u68c0\u67e5\u7684\u503c\uff0cexpression2 \u662f\u5982\u679c expression1 \u4e3a NULL \u65f6\u8981\u8fd4\u56de\u7684\u503c\u3002 3. \u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d expression1 \u548c expression2 \u7684\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u5339\u914d\u3002\u4f8b\u5982\uff0c\u5982\u679c expression1 \u4e3a\u6574\u6570\uff0c\u5219 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6414","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6414","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6414"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6414\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}