{"id":1784,"date":"2025-02-18T22:41:14","date_gmt":"2025-02-18T14:41:14","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1784"},"modified":"2025-02-18T22:41:14","modified_gmt":"2025-02-18T14:41:14","slug":"mysql-null-%e5%80%bc%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/18\/mysql-null-%e5%80%bc%e5%a4%84%e7%90%86\/","title":{"rendered":"MySQL NULL \u503c\u5904\u7406"},"content":{"rendered":"\n<p>\u5728 MySQL \u4e2d\uff0c<code>NULL<\/code> \u4ee3\u8868\u201c\u6ca1\u6709\u503c\u201d\u6216\u201c\u672a\u77e5\u7684\u503c\u201d\uff0c\u5b83\u4e0e\u7a7a\u5b57\u7b26\u4e32 <code>''<\/code>\u3001\u96f6 <code>0<\/code> \u6216\u5176\u4ed6\u4efb\u4f55\u503c\u90fd\u662f\u4e0d\u540c\u7684\u3002\u5904\u7406 <code>NULL<\/code> \u503c\u65f6\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e13\u95e8\u7684 SQL \u51fd\u6570\u548c\u8fd0\u7b97\u7b26\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd MySQL \u4e2d\u7684 <code>NULL<\/code> \u503c\u5904\u7406\uff0c\u5305\u62ec\u5982\u4f55\u67e5\u8be2\u3001\u66f4\u65b0\u3001\u63d2\u5165\u548c\u6bd4\u8f83 <code>NULL<\/code> \u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u63d2\u5165 <code>NULL<\/code> \u503c<\/strong><\/h3>\n\n\n\n<p>\u5728 MySQL \u4e2d\uff0c\u5982\u679c\u5217\u5141\u8bb8 <code>NULL<\/code> \u503c\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u63d2\u5165 <code>NULL<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u8868 <code>employees<\/code>\uff0c\u5176\u4e2d <code>emp_name<\/code> \u548c <code>emp_age<\/code> \u5141\u8bb8\u4e3a <code>NULL<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE employees (\n    emp_id INT PRIMARY KEY,\n    emp_name VARCHAR(100),\n    emp_age INT\n);\n\n-- \u63d2\u5165\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u5c06\u67d0\u4e9b\u5217\u8bbe\u4e3a NULL\nINSERT INTO employees (emp_id, emp_name, emp_age) \nVALUES (1, 'Alice', NULL), \n       (2, 'Bob', 30), \n       (3, NULL, 25);<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>emp_age<\/code> \u5bf9\u4e8e Alice \u662f <code>NULL<\/code>\uff0c<code>emp_name<\/code> \u5bf9\u4e8e\u7b2c\u4e09\u4e2a\u8bb0\u5f55\u662f <code>NULL<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u67e5\u8be2 <code>NULL<\/code> \u503c<\/strong><\/h3>\n\n\n\n<p>\u5728\u67e5\u8be2\u4e2d\uff0c<code>NULL<\/code> \u4e0d\u80fd\u4f7f\u7528\u5e38\u89c4\u7684\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08\u5982 <code>=<\/code>\u3001<code>!=<\/code>\uff09\u6765\u68c0\u67e5\u3002\u76f8\u53cd\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528 <code>IS NULL<\/code> \u6216 <code>IS NOT NULL<\/code> \u6765\u5224\u65ad\u67d0\u4e2a\u503c\u662f\u5426\u4e3a <code>NULL<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2 <code>NULL<\/code> \u503c\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_name, emp_age \nFROM employees \nWHERE emp_age IS NULL;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2\u975e <code>NULL<\/code> \u503c\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_name, emp_age \nFROM employees \nWHERE emp_age IS NOT NULL;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4f7f\u7528 <code>IFNULL()<\/code> \u51fd\u6570<\/strong><\/h3>\n\n\n\n<p><code>IFNULL()<\/code> \u51fd\u6570\u7528\u4e8e\u68c0\u67e5\u67d0\u4e2a\u503c\u662f\u5426\u4e3a <code>NULL<\/code>\uff0c\u5982\u679c\u662f <code>NULL<\/code>\uff0c\u5219\u8fd4\u56de\u6307\u5b9a\u7684\u66ff\u4ee3\u503c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_name, IFNULL(emp_age, 25) AS emp_age \nFROM employees;<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u67e5\u8be2\u4f1a\u5c06 <code>NULL<\/code> \u7684 <code>emp_age<\/code> \u66ff\u6362\u4e3a <code>25<\/code>\uff0c\u8fd4\u56de\u6bcf\u4e2a\u5458\u5de5\u7684\u540d\u5b57\u548c\u5e74\u9f84\u3002\u5982\u679c\u5e74\u9f84\u662f <code>NULL<\/code>\uff0c\u5219\u8fd4\u56de\u9ed8\u8ba4\u503c <code>25<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u4f7f\u7528 <code>COALESCE()<\/code> \u51fd\u6570<\/strong><\/h3>\n\n\n\n<p><code>COALESCE()<\/code> \u51fd\u6570\u8fd4\u56de\u5176\u53c2\u6570\u4e2d\u7b2c\u4e00\u4e2a\u975e <code>NULL<\/code> \u7684\u503c\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u591a\u4e2a\u5217\u6216\u8868\u8fbe\u5f0f\uff0c\u6309\u987a\u5e8f\u68c0\u67e5\u6bcf\u4e2a\u53c2\u6570\uff0c\u76f4\u5230\u627e\u5230\u7b2c\u4e00\u4e2a\u975e <code>NULL<\/code> \u503c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_name, COALESCE(emp_age, 25, 30) AS emp_age \nFROM employees;<\/code><\/pre>\n\n\n\n<p>\u5728\u6b64\u67e5\u8be2\u4e2d\uff0c<code>COALESCE()<\/code> \u4f1a\u68c0\u67e5 <code>emp_age<\/code>\uff0c\u5982\u679c\u662f <code>NULL<\/code>\uff0c\u5219\u8fd4\u56de <code>25<\/code>\uff0c\u5982\u679c <code>25<\/code> \u4e5f\u4e3a <code>NULL<\/code>\uff08\u5047\u8bbe\u6709\u591a\u4e2a\u53c2\u6570\uff09\uff0c\u5219\u8fd4\u56de <code>30<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u4f7f\u7528 <code>NULLIF()<\/code> \u51fd\u6570<\/strong><\/h3>\n\n\n\n<p><code>NULLIF()<\/code> \u51fd\u6570\u5c06\u4e24\u4e2a\u53c2\u6570\u8fdb\u884c\u6bd4\u8f83\uff0c\u5982\u679c\u5b83\u4eec\u76f8\u7b49\uff0c\u5219\u8fd4\u56de <code>NULL<\/code>\uff0c\u5426\u5219\u8fd4\u56de\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002\u53ef\u4ee5\u7528\u6765\u66ff\u6362\u67d0\u4e9b\u503c\u4e3a <code>NULL<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_name, NULLIF(emp_age, 30) AS emp_age \nFROM employees;<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u67e5\u8be2\u4f1a\u5c06\u5e74\u9f84\u4e3a <code>30<\/code> \u7684\u5458\u5de5\u7684 <code>emp_age<\/code> \u5217\u8fd4\u56de <code>NULL<\/code>\uff0c\u5426\u5219\u8fd4\u56de\u539f\u59cb\u5e74\u9f84\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u4f7f\u7528 <code>CASE<\/code> \u8bed\u53e5\u5904\u7406 <code>NULL<\/code><\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>CASE<\/code> \u8868\u8fbe\u5f0f\u6765\u5904\u7406 <code>NULL<\/code> \u503c\uff0c\u4ee5\u4fbf\u6839\u636e <code>NULL<\/code> \u7684\u5b58\u5728\u8fdb\u884c\u66f4\u590d\u6742\u7684\u903b\u8f91\u5904\u7406\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_name, \n       CASE \n           WHEN emp_age IS NULL THEN 'Age not provided' \n           ELSE CONCAT(emp_age, ' years old') \n       END AS emp_age\nFROM employees;<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u67e5\u8be2\u4e2d\uff0c\u5982\u679c <code>emp_age<\/code> \u4e3a <code>NULL<\/code>\uff0c\u5219\u663e\u793a\u4e3a <code>'Age not provided'<\/code>\uff0c\u5426\u5219\u663e\u793a\u5e74\u9f84\u4fe1\u606f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u66f4\u65b0 <code>NULL<\/code> \u503c<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u9700\u8981\u66f4\u65b0\u67d0\u4e00\u5217\u4e3a <code>NULL<\/code>\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728 <code>UPDATE<\/code> \u8bed\u53e5\u4e2d\u4f7f\u7528 <code>NULL<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE employees \nSET emp_age = NULL \nWHERE emp_name = 'Alice';<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4f1a\u5c06 Alice \u7684\u5e74\u9f84\u66f4\u65b0\u4e3a <code>NULL<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong><code>NULL<\/code> \u503c\u4e0e\u6392\u5e8f<\/strong><\/h3>\n\n\n\n<p>\u5728\u8fdb\u884c\u6392\u5e8f\u65f6\uff0c<code>NULL<\/code> \u503c\u4f1a\u88ab\u8ba4\u4e3a\u662f\u201c\u6700\u5c0f\u201d\u6216\u201c\u6700\u5927\u201d\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u6392\u5e8f\u987a\u5e8f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u9ed8\u8ba4\u6392\u5e8f\uff08\u5347\u5e8f\uff0c<code>NULL<\/code> \u4f5c\u4e3a\u6700\u5c0f\u503c\uff09\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_name, emp_age \nFROM employees \nORDER BY emp_age ASC;<\/code><\/pre>\n\n\n\n<p>\u5728\u5347\u5e8f\u6392\u5217\u65f6\uff0c<code>NULL<\/code> \u4f1a\u6392\u5728\u6700\u524d\u9762\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>NULL<\/code> \u4f5c\u4e3a\u6700\u5927\u503c\uff1a<\/h4>\n\n\n\n<p>\u5982\u679c\u4f60\u5e0c\u671b <code>NULL<\/code> \u6392\u5728\u6700\u5927\u503c\u4e4b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>ORDER BY<\/code> \u7684\u7279\u6b8a\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_name, emp_age \nFROM employees \nORDER BY emp_age IS NULL, emp_age ASC;<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4f1a\u5148\u5c06 <code>NULL<\/code> \u6392\u5728\u6700\u540e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9. <strong><code>NULL<\/code> \u503c\u5728\u805a\u5408\u51fd\u6570\u4e2d\u7684\u5904\u7406<\/strong><\/h3>\n\n\n\n<p>\u805a\u5408\u51fd\u6570\uff08\u5982 <code>COUNT()<\/code>\u3001<code>SUM()<\/code>\u3001<code>AVG()<\/code> \u7b49\uff09\u5728\u5904\u7406 <code>NULL<\/code> \u503c\u65f6\u6709\u7279\u5b9a\u7684\u89c4\u5219\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>COUNT()<\/code> \u4e0d\u4f1a\u8ba1\u7b97 <code>NULL<\/code> \u503c\u3002<\/li>\n\n\n\n<li><code>SUM()<\/code> \u548c <code>AVG()<\/code> \u4f1a\u5ffd\u7565 <code>NULL<\/code> \u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COUNT(emp_age) AS num_employees_with_age,\n       SUM(emp_age) AS total_age,\n       AVG(emp_age) AS average_age\nFROM employees;<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u67e5\u8be2\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>COUNT(emp_age)<\/code> \u4f1a\u8ba1\u7b97\u975e <code>NULL<\/code> \u7684\u8bb0\u5f55\u6570\u91cf\u3002<\/li>\n\n\n\n<li><code>SUM(emp_age)<\/code> \u4f1a\u5ffd\u7565 <code>NULL<\/code> \u503c\u8fdb\u884c\u6c42\u548c\u3002<\/li>\n\n\n\n<li><code>AVG(emp_age)<\/code> \u4f1a\u5ffd\u7565 <code>NULL<\/code> \u503c\u8ba1\u7b97\u5e73\u5747\u5e74\u9f84\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">10. <strong><code>NULL<\/code> \u503c\u4e0e\u6bd4\u8f83\u8fd0\u7b97\u7b26<\/strong><\/h3>\n\n\n\n<p>\u5728 MySQL \u4e2d\uff0c<code>NULL<\/code> \u503c\u7684\u6bd4\u8f83\u9700\u8981\u7279\u6b8a\u5904\u7406\uff0c\u56e0\u4e3a <code>NULL<\/code> \u4e0d\u80fd\u4e0e\u5176\u4ed6\u503c\u76f4\u63a5\u6bd4\u8f83\u3002\u4efb\u4f55\u4e0e <code>NULL<\/code> \u7684\u76f4\u63a5\u6bd4\u8f83\uff08\u5982 <code>=<\/code> \u6216 <code>!=<\/code>\uff09\u90fd\u4f1a\u8fd4\u56de <code>FALSE<\/code>\u3002\u56e0\u6b64\uff0c\u4f60\u9700\u8981\u4f7f\u7528 <code>IS NULL<\/code> \u6216 <code>IS NOT NULL<\/code> \u6765\u8fdb\u884c\u68c0\u67e5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u9519\u8bef\u7684\u6bd4\u8f83\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM employees WHERE emp_age = NULL; -- \u9519\u8bef<\/code><\/pre>\n\n\n\n<p>\u8fd9\u5c06\u59cb\u7ec8\u8fd4\u56de\u7a7a\u7ed3\u679c\uff0c\u56e0\u4e3a\u4efb\u4f55\u503c\u4e0e <code>NULL<\/code> \u6bd4\u8f83\u90fd\u4f1a\u8fd4\u56de <code>FALSE<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u786e\u7684\u6bd4\u8f83\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM employees WHERE emp_age IS NULL; -- \u6b63\u786e<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>NULL<\/code> \u5728 MySQL \u4e2d\u8868\u793a\u201c\u65e0\u503c\u201d\u6216\u201c\u672a\u77e5\u7684\u503c\u201d\u3002<\/li>\n\n\n\n<li>\u63d2\u5165\u3001\u67e5\u8be2\u3001\u66f4\u65b0\u548c\u6392\u5e8f\u65f6\u9700\u8981\u6ce8\u610f <code>NULL<\/code> \u7684\u7279\u6b8a\u5904\u7406\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>IS NULL<\/code> \u6216 <code>IS NOT NULL<\/code> \u6765\u5224\u65ad <code>NULL<\/code> \u503c\uff0c\u907f\u514d\u4f7f\u7528\u5e38\u89c4\u7684\u6bd4\u8f83\u8fd0\u7b97\u7b26\u3002<\/li>\n\n\n\n<li><code>IFNULL()<\/code> \u548c <code>COALESCE()<\/code> \u7b49\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u66ff\u6362 <code>NULL<\/code> \u503c\u3002<\/li>\n\n\n\n<li>\u805a\u5408\u51fd\u6570\u901a\u5e38\u4f1a\u5ffd\u7565 <code>NULL<\/code> \u503c\uff0c\u4f46 <code>COUNT()<\/code> \u53ef\u4ee5\u7edf\u8ba1\u975e <code>NULL<\/code> \u503c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7\u7075\u6d3b\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u548c\u6280\u5de7\uff0c\u4f60\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u5904\u7406 MySQL \u4e2d\u7684 <code>NULL<\/code> \u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 MySQL \u4e2d\uff0cNULL \u4ee3\u8868\u201c\u6ca1\u6709\u503c\u201d\u6216\u201c\u672a\u77e5\u7684\u503c\u201d\uff0c\u5b83\u4e0e\u7a7a\u5b57\u7b26\u4e32 &#8221;\u3001\u96f6 0 \u6216\u5176\u4ed6\u4efb\u4f55\u503c\u90fd\u662f\u4e0d\u540c\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1784","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1784"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1784\/revisions"}],"predecessor-version":[{"id":1785,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1784\/revisions\/1785"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}