{"id":6423,"date":"2024-11-14T14:34:38","date_gmt":"2024-11-14T06:34:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6423\/"},"modified":"2024-11-14T14:34:38","modified_gmt":"2024-11-14T06:34:38","slug":"mysql%e6%9f%a5%e8%af%a2%e4%b8%8d%e4%b8%ba%e7%a9%ba%e7%9a%84%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6423\/","title":{"rendered":"mysql\u67e5\u8be2\u4e0d\u4e3a\u7a7a\u7684\u6570\u636e"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7\u4f7f\u7528 is not null \u6761\u4ef6\uff0c\u53ef\u4ee5\u67e5\u8be2 mysql \u4e2d\u4e0d\u4e3a\u7a7a\u7684\u6570\u636e\u3002\u5176\u4ed6\u65b9\u6cd5\u5305\u62ec coalesce \u51fd\u6570\u3001case \u8868\u8fbe\u5f0f\u548c not in \u67e5\u8be2\u3002\u9009\u62e9\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u60c5\u51b5\u548c\u6027\u80fd\u8981\u6c42\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/02\/2024080207453918872.jpg\" class=\"aligncenter\" title=\"mysql\u67e5\u8be2\u4e0d\u4e3a\u7a7a\u7684\u6570\u636e\u63d2\u56fe\" alt=\"mysql\u67e5\u8be2\u4e0d\u4e3a\u7a7a\u7684\u6570\u636e\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u67e5\u8be2 MySQL \u4e2d\u4e0d\u4e3a\u7a7a\u7684\u6570\u636e<\/strong><\/p>\n<p>\u8981\u67e5\u8be2 MySQL \u4e2d\u4e0d\u4e3a\u7a7a\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 IS NOT NULL \u6761\u4ef6\u3002\u8be5\u6761\u4ef6\u68c0\u67e5\u6307\u5b9a\u7684\u5217\u662f\u5426\u4e0d\u5305\u542b NULL \u503c\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>SELECT *\nFROM table_name\nWHERE column_name IS NOT NULL;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u67e5\u8be2 customers \u8868\u4e2d name \u5217\u4e0d\u4e3a\u7a7a\u7684\u6240\u6709\u8bb0\u5f55\uff1a<\/p>\n<pre>SELECT *\nFROM customers\nWHERE name IS NOT NULL;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u65b9\u6cd5<\/strong><\/p>\n<p>\u9664\u4e86 IS NOT NULL \u6761\u4ef6\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u67e5\u8be2\u4e0d\u4e3a\u7a7a\u7684\u6570\u636e\uff1a<\/p>\n<ul>\n<li> <strong>COALESCE \u51fd\u6570\uff1a<\/strong> \u5c06\u4e00\u4e2a\u6216\u591a\u4e2a\u8868\u8fbe\u5f0f\u6c42\u503c\u4e3a\u7b2c\u4e00\u4e2a\u975e\u7a7a\u8868\u8fbe\u5f0f\u3002<\/li>\n<\/ul>\n<pre>SELECT COALESCE(column1, column2, ...)\nFROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>CASE \u8868\u8fbe\u5f0f\uff1a<\/strong> \u57fa\u4e8e\u67d0\u4e2a\u6761\u4ef6\u8fd4\u56de\u4e0d\u540c\u7684\u503c\u3002<\/li>\n<\/ul>\n<pre>SELECT CASE\n    WHEN column_name IS NOT NULL\n    THEN column_name\n    ELSE 'NULL'\nEND\nFROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>NOT IN \u67e5\u8be2\uff1a<\/strong> \u8fd4\u56de\u4e0d\u5728\u6307\u5b9a\u503c\u5217\u8868\u4e2d\u7684\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n<pre>SELECT *\nFROM table_name\nWHERE column_name NOT IN (NULL);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u60c5\u51b5\u548c\u6027\u80fd\u8981\u6c42\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u67e5\u8be2\uff0cIS NOT NULL \u6761\u4ef6\u901a\u5e38\u662f\u6700\u5feb\u7684\u9009\u62e9\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u60c5\u51b5\uff0c\u5176\u4ed6\u65b9\u6cd5\u53ef\u80fd\u66f4\u9002\u5408\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u67e5\u8be2\u4e0d\u4e3a\u7a7a\u7684\u6570\u636e\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>\u901a\u8fc7\u4f7f\u7528 is not null \u6761\u4ef6\uff0c\u53ef\u4ee5\u67e5\u8be2 mysql \u4e2d\u4e0d\u4e3a\u7a7a\u7684\u6570\u636e\u3002\u5176\u4ed6\u65b9\u6cd5\u5305\u62ec coalesce \u51fd\u6570\u3001case \u8868\u8fbe\u5f0f\u548c not in \u67e5\u8be2\u3002\u9009\u62e9\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u60c5\u51b5\u548c\u6027\u80fd\u8981\u6c42\u3002 \u5982\u4f55\u67e5\u8be2 MySQL \u4e2d\u4e0d\u4e3a\u7a7a\u7684\u6570\u636e \u8981\u67e5\u8be2 MySQL \u4e2d\u4e0d\u4e3a\u7a7a\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 IS NOT NULL \u6761\u4ef6\u3002\u8be5\u6761\u4ef6\u68c0\u67e5\u6307\u5b9a\u7684\u5217\u662f\u5426\u4e0d\u5305\u542b NULL \u503c\u3002 \u8bed\u6cd5 SELECT * FROM table_name WHERE column_name IS NOT NULL; \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0b\u793a\u4f8b\u67e5\u8be2 customers \u8868\u4e2d name \u5217\u4e0d\u4e3a\u7a7a\u7684\u6240\u6709\u8bb0\u5f55\uff1a SELECT * FROM customers WHERE name IS NOT NULL; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4ed6\u65b9\u6cd5 \u9664\u4e86 IS NOT [&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-6423","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6423","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=6423"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6423\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}