{"id":1713,"date":"2025-02-16T08:41:09","date_gmt":"2025-02-16T00:41:09","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1713"},"modified":"2025-02-16T08:41:09","modified_gmt":"2025-02-16T00:41:09","slug":"count-%e5%87%bd%e6%95%b0%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98%e5%8f%8a%e8%a7%a3%e5%86%b3%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/16\/count-%e5%87%bd%e6%95%b0%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98%e5%8f%8a%e8%a7%a3%e5%86%b3%e6%8c%87%e5%8d%97\/","title":{"rendered":"COUNT() \u51fd\u6570\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u6307\u5357"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">SQL \u4e2d\u7684 <code>COUNT()<\/code> \u51fd\u6570\u5728\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u3002\u8fd9\u91cc\u5217\u51fa\u4e86\u4e00\u4e9b\u5e38\u89c1\u7684\u60c5\u51b5\u548c\u76f8\u5e94\u7684\u89e3\u51b3\u529e\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u95ee\u9898\uff1a<code>COUNT(*)<\/code> \u7ed3\u679c\u6bd4\u9884\u671f\u5c11<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u539f\u56e0\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f7f\u7528\u4e86 <code>WHERE<\/code> \u5b50\u53e5\u8fc7\u6ee4\u6570\u636e\uff0c<code>COUNT(*)<\/code> \u53ea\u4f1a\u7edf\u8ba1\u7b26\u5408\u6761\u4ef6\u7684\u884c\uff0c\u800c\u4e0d\u662f\u8868\u4e2d\u6240\u6709\u7684\u884c\u3002\u5982\u679c\u6ca1\u6709\u5199\u6e05\u695a\u7b5b\u9009\u6761\u4ef6\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9057\u6f0f\u4e00\u4e9b\u884c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u68c0\u67e5 <code>WHERE<\/code> \u5b50\u53e5\u662f\u5426\u6b63\u786e\uff0c\u786e\u4fdd\u8fc7\u6ee4\u6761\u4ef6\u5408\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COUNT(*) FROM employees WHERE department = 'Sales';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c <code>Sales<\/code> \u90e8\u95e8\u6ca1\u6709\u5458\u5de5\uff0c\u7ed3\u679c\u5c06\u4f1a\u662f <code>0<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u95ee\u9898\uff1a<code>COUNT(column_name)<\/code> \u8ba1\u7b97\u503c\u5c11\u4e8e\u9884\u671f<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u539f\u56e0\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>COUNT(column_name)<\/code> \u53ea\u7edf\u8ba1\u975e <code>NULL<\/code> \u503c\u7684\u884c\u3002\u5982\u679c\u5217\u4e2d\u6709\u5f88\u591a <code>NULL<\/code> \u503c\uff0c\u7edf\u8ba1\u7684\u6570\u91cf\u5c31\u4f1a\u6bd4\u4f60\u9884\u671f\u7684\u5c11\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5e0c\u671b\u8ba1\u7b97\u6240\u6709\u884c\u7684\u6570\u91cf\uff0c\u5305\u62ec <code>NULL<\/code> \u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>COUNT(*)<\/code> \u800c\u4e0d\u662f <code>COUNT(column_name)<\/code>\u3002\u5982\u679c\u4f60\u9700\u8981\u8ba1\u7b97\u67d0\u5217\u7684\u6240\u6709\u503c\uff08\u5305\u62ec <code>NULL<\/code>\uff09\uff0c\u8003\u8651\u4f7f\u7528 <code>COUNT(*)<\/code> \u800c\u4e0d\u662f <code>COUNT(column_name)<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COUNT(*) FROM employees;  -- \u8ba1\u7b97\u6240\u6709\u884c<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u95ee\u9898\uff1a<code>COUNT(DISTINCT column_name)<\/code> \u7ed3\u679c\u4e0d\u51c6\u786e<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u539f\u56e0\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>COUNT(DISTINCT column_name)<\/code> \u8ba1\u7b97\u7684\u662f\u67d0\u5217\u7684\u4e0d\u540c\u503c\u7684\u6570\u91cf\u3002\u5982\u679c\u4f60\u9047\u5230\u7684\u7ed3\u679c\u6bd4\u9884\u671f\u5c11\uff0c\u53ef\u80fd\u662f\u7531\u4e8e\u91cd\u590d\u503c\u6216\u8005 <code>NULL<\/code> \u503c\u9020\u6210\u7684\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u68c0\u67e5\u8be5\u5217\u662f\u5426\u5305\u542b\u610f\u5916\u7684\u91cd\u590d\u503c\u6216\u4e0d\u5408\u89c4\u6570\u636e\u3002<\/li>\n\n\n\n<li><code>NULL<\/code> \u503c\u4e5f\u4f1a\u88ab\u89c6\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u503c\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u6392\u9664 <code>NULL<\/code>\uff0c\u53ef\u4ee5\u52a0\u4e00\u4e2a\u6761\u4ef6\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COUNT(DISTINCT column_name) FROM table_name WHERE column_name IS NOT NULL;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u95ee\u9898\uff1a<code>COUNT()<\/code> \u548c <code>GROUP BY<\/code> \u642d\u914d\u4f7f\u7528\u65f6\u7ed3\u679c\u4e0d\u51c6\u786e<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u539f\u56e0\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>GROUP BY<\/code> \u5b50\u53e5\u4e2d\uff0c\u5982\u679c\u67d0\u4e9b\u5206\u7ec4\u6ca1\u6709\u6570\u636e\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u8fd9\u4e9b\u5206\u7ec4\u4f1a\u88ab\u5ffd\u7565\u3002\u56e0\u6b64\uff0c\u67d0\u4e9b\u9884\u671f\u7684\u5206\u7ec4\u53ef\u80fd\u4f1a\u4e22\u5931\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 <code>HAVING<\/code> \u5b50\u53e5\u786e\u4fdd\u5728\u5206\u7ec4\u540e\u8fc7\u6ee4\u7ed3\u679c\u65f6\u8003\u8651\u5230\u6240\u6709\u5206\u7ec4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT department, COUNT(*) \nFROM employees\nGROUP BY department\nHAVING COUNT(*) &gt; 0;  -- \u53ea\u663e\u793a\u6709\u5458\u5de5\u7684\u90e8\u95e8<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u95ee\u9898\uff1a<code>COUNT()<\/code> \u5728\u590d\u6742\u67e5\u8be2\u4e2d\u8fd4\u56de\u9519\u8bef<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u539f\u56e0\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u5728\u67e5\u8be2\u4e2d\u4f7f\u7528\u4e86\u590d\u6742\u7684\u8054\u63a5\uff08JOIN\uff09\u6216\u5b50\u67e5\u8be2\uff0c<code>COUNT()<\/code> \u7684\u4f7f\u7528\u53ef\u80fd\u4f1a\u53d7\u5230\u5f71\u54cd\u3002\u5c24\u5176\u662f\u8054\u63a5\u8868\u65f6\uff0c\u91cd\u590d\u8bb0\u5f55\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u7684\u8ba1\u6570\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u786e\u4fdd\u8054\u63a5\u6761\u4ef6\uff08JOIN \u6761\u4ef6\uff09\u6b63\u786e\uff0c\u907f\u514d\u91cd\u590d\u8bb0\u5f55\u3002\u5982\u679c\u6709\u53ef\u80fd\u51fa\u73b0\u91cd\u590d\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>DISTINCT<\/code> \u6765\u6392\u9664\u91cd\u590d\u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COUNT(DISTINCT employee_id)\nFROM employees\nJOIN departments ON employees.department_id = departments.department_id;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u95ee\u9898\uff1a<code>COUNT()<\/code> \u4e0e <code>NULL<\/code> \u7684\u884c\u4e3a\u4e0d\u6e05\u695a<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u539f\u56e0\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>COUNT()<\/code> \u4f1a\u81ea\u52a8\u5ffd\u7565 <code>NULL<\/code> \u503c\u3002\u8fd9\u53ef\u80fd\u5bfc\u81f4\u7528\u6237\u5bf9 <code>COUNT()<\/code> \u7684\u8fd4\u56de\u7ed3\u679c\u611f\u5230\u56f0\u60d1\uff0c\u7279\u522b\u662f\u5f53 <code>NULL<\/code> \u503c\u8f83\u591a\u65f6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u4f60\u9700\u8981\u5305\u542b <code>NULL<\/code> \u503c\uff0c\u8003\u8651\u4f7f\u7528 <code>COUNT(*)<\/code> \u6765\u8ba1\u7b97\u6240\u6709\u884c\u6570\uff0c\u800c\u4e0d\u662f\u4f7f\u7528 <code>COUNT(column_name)<\/code>\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u4f60\u60f3\u7279\u522b\u6392\u9664 <code>NULL<\/code> \u503c\uff0c\u8bf7\u5728 <code>WHERE<\/code> \u5b50\u53e5\u4e2d\u6307\u5b9a\u6761\u4ef6\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COUNT(*) \nFROM employees\nWHERE salary IS NOT NULL;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u95ee\u9898\uff1a<code>COUNT()<\/code> \u4e0d\u9002\u7528\u4e8e\u7a7a\u8868<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u539f\u56e0\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8868\u4e2d\u6ca1\u6709\u6570\u636e\uff0c<code>COUNT(*)<\/code> \u4f1a\u8fd4\u56de <code>0<\/code>\uff0c\u8fd9\u53ef\u80fd\u662f\u4f60\u9884\u671f\u4e4b\u5916\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e1a\u52a1\u903b\u8f91\u4e2d\uff0c\u53ef\u4ee5\u6dfb\u52a0\u6761\u4ef6\u5224\u65ad\u6765\u5904\u7406\u7a7a\u8868\u7684\u7279\u6b8a\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u68c0\u67e5\u67e5\u8be2\u7ed3\u679c\u662f\u5426\u4e3a\u7a7a\u5e76\u505a\u76f8\u5e94\u7684\u5904\u7406\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COUNT(*) AS total_count\nFROM employees;\n-- \u5982\u679c\u8fd4\u56de\u503c\u662f 0\uff0c\u8868\u660e\u8868\u4e3a\u7a7a\uff0c\u505a\u76f8\u5e94\u5904\u7406<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong>\u95ee\u9898\uff1a\u67e5\u8be2\u6548\u7387\u4f4e\uff0c<code>COUNT()<\/code> \u5728\u5927\u6570\u636e\u96c6\u4e0a\u8868\u73b0\u4e0d\u597d<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u539f\u56e0\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>COUNT()<\/code> \u662f\u4e00\u4e2a\u805a\u5408\u51fd\u6570\uff0c\u9700\u8981\u5bf9\u6bcf\u4e00\u884c\u8fdb\u884c\u8ba1\u7b97\uff0c\u5c24\u5176\u662f\u5728\u6ca1\u6709\u7d22\u5f15\u7684\u5217\u4e0a\u8fdb\u884c\u8ba1\u7b97\u65f6\uff0c\u53ef\u80fd\u4f1a\u975e\u5e38\u6162\u3002\u5bf9\u4e8e\u5927\u6570\u636e\u8868\u6765\u8bf4\uff0c\u8ba1\u7b97\u65f6\u95f4\u53ef\u80fd\u975e\u5e38\u957f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u786e\u4fdd\u8ba1\u7b97\u7684\u5217\u4e0a\u6709\u7d22\u5f15\uff0c\u7279\u522b\u662f <code>WHERE<\/code> \u5b50\u53e5\u4e2d\u7ecf\u5e38\u67e5\u8be2\u7684\u5217\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u53ea\u662f\u4e3a\u4e86\u8ba1\u7b97\u884c\u6570\uff0c\u53ef\u4ee5\u5728\u8868\u7684\u5143\u6570\u636e\u4e2d\u7ef4\u62a4\u8ba1\u6570\uff0c\u5b9a\u671f\u66f4\u65b0\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u9891\u7e41\u67e5\u8be2\u8ba1\u7b97\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>COUNT()<\/code> \u51fd\u6570\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u90fd\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5728\u590d\u6742\u7684\u67e5\u8be2\u548c\u5927\u6570\u636e\u96c6\u4e0a\uff0c\u4f7f\u7528\u65f6\u9700\u8981\u7279\u522b\u6ce8\u610f\u3002\u5e38\u89c1\u7684\u95ee\u9898\u4e00\u822c\u4e0e <code>NULL<\/code> \u503c\u3001\u8054\u63a5\u8868\u65f6\u7684\u91cd\u590d\u6570\u636e\u3001\u4ee5\u53ca\u6ca1\u6709\u6b63\u786e\u8fc7\u6ee4\u6761\u4ef6\u6709\u5173\u3002\u786e\u4fdd\u67e5\u8be2\u6761\u4ef6\u6b63\u786e\uff0c\u7406\u89e3 <code>COUNT()<\/code> \u7684\u7279\u6027\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL \u4e2d\u7684 COUNT() \u51fd\u6570\u5728\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u3002\u8fd9\u91cc\u5217\u51fa\u4e86\u4e00\u4e9b\u5e38\u89c1\u7684\u60c5\u51b5\u548c\u76f8\u5e94\u7684\u89e3\u51b3\u529e\u6cd5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-1713","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1713","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=1713"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1713\/revisions"}],"predecessor-version":[{"id":1714,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1713\/revisions\/1714"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}