{"id":1711,"date":"2025-02-16T08:40:03","date_gmt":"2025-02-16T00:40:03","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1711"},"modified":"2025-02-16T08:40:03","modified_gmt":"2025-02-16T00:40:03","slug":"sql-count-%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/16\/sql-count-%e5%87%bd%e6%95%b0\/","title":{"rendered":"SQL COUNT() \u51fd\u6570"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"> <code>COUNT()<\/code> \u51fd\u6570\uff0c\u5b83\u662f SQL \u4e2d\u7684\u53e6\u4e00\u4e2a\u5e38\u7528\u805a\u5408\u51fd\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u67d0\u4e2a\u5b57\u6bb5\u6216\u8868\u4e2d\u884c\u7684\u6570\u91cf\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <code>COUNT()<\/code> \u51fd\u6570\u7b80\u4ecb<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>COUNT()<\/code> \u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u7b26\u5408\u6761\u4ef6\u7684\u884c\u6570\u3002\u5b83\u53ef\u4ee5\u8ba1\u7b97\u67d0\u4e2a\u5217\u4e2d\u975e <code>NULL<\/code> \u503c\u7684\u6570\u91cf\uff0c\u6216\u8005\u8ba1\u7b97\u7b26\u5408\u6307\u5b9a\u6761\u4ef6\u7684\u603b\u884c\u6570\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u57fa\u672c\u8bed\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COUNT(column_name)\nFROM table_name\nWHERE condition;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>column_name<\/code>\uff1a\u7528\u4e8e\u8ba1\u6570\u7684\u5217\u3002\u5982\u679c\u6307\u5b9a\u7684\u5217\u4e2d\u6709 <code>NULL<\/code> \u503c\uff0c<code>COUNT(column_name)<\/code> \u4f1a\u5ffd\u7565\u8fd9\u4e9b\u503c\u3002<\/li>\n\n\n\n<li><code>table_name<\/code>\uff1a\u6570\u636e\u8868\u7684\u540d\u5b57\u3002<\/li>\n\n\n\n<li><code>condition<\/code>\uff1a\u53ef\u9009\uff0c\u7528\u4e8e\u8fc7\u6ee4\u6570\u636e\u7684\u6761\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5e0c\u671b\u8ba1\u7b97\u8868\u4e2d\u7684\u6240\u6709\u884c\u6570\uff0c\u800c\u4e0d\u8003\u8651\u67d0\u5217\u7684 <code>NULL<\/code> \u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>COUNT(*)<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u793a\u4f8b 1\uff1a\u8ba1\u7b97\u8868\u4e2d\u7684\u884c\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5458\u5de5\u8868 <code>employees<\/code>\uff0c\u8868\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>employee_id<\/th><th>name<\/th><th>salary<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Tom<\/td><td>5000<\/td><\/tr><tr><td>2<\/td><td>Jane<\/td><td>6000<\/td><\/tr><tr><td>3<\/td><td>Bob<\/td><td>7000<\/td><\/tr><tr><td>4<\/td><td>Alice<\/td><td>8000<\/td><\/tr><tr><td>5<\/td><td>Mike<\/td><td>NULL<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>COUNT(*)<\/code> \u6765\u8ba1\u7b97\u8868\u4e2d\u6240\u6709\u884c\u7684\u6570\u91cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COUNT(*) AS total_employees\nFROM employees;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>total_employees<\/th><\/tr><\/thead><tbody><tr><td>5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u793a\u4f8b 2\uff1a\u8ba1\u7b97\u67d0\u5217\u975e <code>NULL<\/code> \u503c\u7684\u884c\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u8ba1\u7b97 <code>salary<\/code> \u5217\u4e2d\u975e <code>NULL<\/code> \u503c\u7684\u6570\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>COUNT(salary)<\/code>\u3002\u5b83\u53ea\u4f1a\u8ba1\u7b97 <code>salary<\/code> \u5217\u4e2d\u6709\u5b9e\u9645\u503c\u7684\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COUNT(salary) AS non_null_salaries\nFROM employees;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>non_null_salaries<\/th><\/tr><\/thead><tbody><tr><td>4<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u793a\u4f8b 3\uff1a\u4f7f\u7528 <code>COUNT()<\/code> \u7ed3\u5408 <code>GROUP BY<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u8fd8\u53ef\u4ee5\u5c06 <code>COUNT()<\/code> \u51fd\u6570\u4e0e <code>GROUP BY<\/code> \u5b50\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u6309\u67d0\u4e2a\u5b57\u6bb5\u5206\u7ec4\u7edf\u8ba1\u6bcf\u7ec4\u7684\u884c\u6570\u3002\u4f8b\u5982\uff0c\u6309\u90e8\u95e8\u7edf\u8ba1\u5458\u5de5\u6570\u91cf\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe <code>employees<\/code> \u8868\u4e2d\u6709\u4e00\u4e2a <code>department<\/code> \u5b57\u6bb5\uff0c\u8bb0\u5f55\u5458\u5de5\u6240\u5c5e\u7684\u90e8\u95e8\uff0c\u4f60\u53ef\u4ee5\u8ba1\u7b97\u6bcf\u4e2a\u90e8\u95e8\u7684\u5458\u5de5\u6570\u91cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT department, COUNT(*) AS employee_count\nFROM employees\nGROUP BY department;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>department<\/th><th>employee_count<\/th><\/tr><\/thead><tbody><tr><td>Sales<\/td><td>2<\/td><\/tr><tr><td>HR<\/td><td>1<\/td><\/tr><tr><td>IT<\/td><td>2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u793a\u4f8b 4\uff1a\u4f7f\u7528 <code>COUNT()<\/code> \u548c <code>HAVING<\/code> \u8fdb\u884c\u6761\u4ef6\u7b5b\u9009<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>HAVING<\/code> \u5b50\u53e5\u7528\u4e8e\u8fc7\u6ee4 <code>GROUP BY<\/code> \u805a\u5408\u7ed3\u679c\u3002\u5047\u8bbe\u4f60\u53ea\u60f3\u7edf\u8ba1\u5458\u5de5\u6570\u5927\u4e8e 1 \u7684\u90e8\u95e8\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT department, COUNT(*) AS employee_count\nFROM employees\nGROUP BY department\nHAVING COUNT(*) &gt; 1;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>department<\/th><th>employee_count<\/th><\/tr><\/thead><tbody><tr><td>Sales<\/td><td>2<\/td><\/tr><tr><td>IT<\/td><td>2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">7. \u793a\u4f8b 5\uff1a\u8ba1\u7b97\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u884c\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u4f60\u53ea\u60f3\u7edf\u8ba1\u5de5\u8d44\u5927\u4e8e 6000 \u7684\u5458\u5de5\u6570\u91cf\uff0c\u53ef\u4ee5\u5728 <code>WHERE<\/code> \u5b50\u53e5\u4e2d\u6dfb\u52a0\u6761\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COUNT(*) AS high_salary_count\nFROM employees\nWHERE salary &gt; 6000;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>high_salary_count<\/th><\/tr><\/thead><tbody><tr><td>3<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">8. \u8fdb\u9636\u7528\u6cd5\uff1a<code>COUNT()<\/code> \u4e0e <code>DISTINCT<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>COUNT(DISTINCT column_name)<\/code> \u7528\u4e8e\u8ba1\u7b97\u67d0\u5217\u4e2d\u4e0d\u540c\u503c\u7684\u6570\u91cf\u3002\u8fd9\u5728\u9700\u8981\u8ba1\u7b97\u4e0d\u91cd\u590d\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u8ba1\u7b97\u4e0d\u540c\u5de5\u8d44\u7684\u5458\u5de5\u6570\u91cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COUNT(DISTINCT salary) AS unique_salaries\nFROM employees;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>unique_salaries<\/th><\/tr><\/thead><tbody><tr><td>4<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>COUNT()<\/code> \u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u805a\u5408\u51fd\u6570\uff0c\u5e38\u7528\u4e8e\u8ba1\u7b97\u884c\u6570\u3001\u975e <code>NULL<\/code> \u503c\u7684\u6570\u91cf\uff0c\u6216\u8005\u6309\u7279\u5b9a\u6761\u4ef6\u7edf\u8ba1\u6570\u636e\u3002\u5b83\u4e0e <code>GROUP BY<\/code> \u548c <code>HAVING<\/code> \u7ed3\u5408\u4f7f\u7528\u65f6\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>COUNT() \u51fd\u6570\uff0c\u5b83\u662f SQL \u4e2d\u7684\u53e6\u4e00\u4e2a\u5e38\u7528\u805a\u5408\u51fd\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u67d0\u4e2a\u5b57\u6bb5\u6216\u8868\u4e2d\u884c\u7684\u6570\u91cf\u3002 1. COUNT [&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-1711","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1711","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=1711"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1711\/revisions"}],"predecessor-version":[{"id":1712,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1711\/revisions\/1712"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}