{"id":5714,"date":"2024-11-14T08:26:11","date_gmt":"2024-11-14T00:26:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5714\/"},"modified":"2024-11-14T08:26:11","modified_gmt":"2024-11-14T00:26:11","slug":"sql%e4%b8%adcount%e5%87%bd%e6%95%b0%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5714\/","title":{"rendered":"sql\u4e2dcount\u51fd\u6570\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  sql count() \u51fd\u6570\u7528\u4e8e\u7edf\u8ba1\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u884c\u6570\uff0c\u8bed\u6cd5\u4e3a count([distinct] column_name)\u3002\u53ef\u7528\u4e8e\u7edf\u8ba1\u8868\u4e2d\u6240\u6709\u884c\u7684\u6570\u91cf\u3001\u7279\u5b9a\u5217\u4e0a\u7684\u4e0d\u91cd\u590d\u884c\u7684\u6570\u91cf\u6216\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\u6570\u3002count() \u5728\u8ba1\u7b97\u7a7a\u503c\u65f6\u8fd4\u56de 0\uff0c\u9664\u975e\u4f7f\u7528 count(*)\uff0c\u5b83\u5c06\u8ba1\u6570\u6240\u6709\u884c\u3002count(distinct) \u51fd\u6570\u5ffd\u7565\u91cd\u590d\u503c\uff0c\u53ea\u8ba1\u7b97\u552f\u4e00\u503c\u3002\u53ef\u4ee5\u4e0e\u5176\u4ed6\u805a\u5408\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053021152263461.jpg\" class=\"aligncenter\" title=\"sql\u4e2dcount\u51fd\u6570\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"sql\u4e2dcount\u51fd\u6570\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL COUNT() \u51fd\u6570\u7684\u4f7f\u7528\u6307\u5357<\/strong><\/p>\n<p>SQL COUNT() \u51fd\u6570\u7528\u4e8e\u7edf\u8ba1\u8868\u4e2d\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u884c\u6570\u3002\u5b83\u662f\u4e00\u4e2a\uff0c\u5373\u5b83\u5c06\u591a\u4e2a\u503c\u7ec4\u5408\u4e3a\u4e00\u4e2a\u5355\u4e2a\u503c\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>COUNT([DISTINCT] column_name)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u53c2\u6570\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>DISTINCT (\u53ef\u9009)\uff1a<\/strong> \u6307\u5b9a\u53ea\u7edf\u8ba1\u552f\u4e00\u503c\u3002<\/li>\n<li> <strong>column_name\uff1a<\/strong> \u8981\u7edf\u8ba1\u7684\u884c\u6216\u5217\u7684\u540d\u79f0\u3002<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/strong><\/p>\n<ol>\n<li><strong>\u7edf\u8ba1\u8868\u4e2d\u6240\u6709\u884c\u7684\u6570\u91cf\uff1a<\/strong><\/li>\n<\/ol>\n<pre>SELECT COUNT(*) FROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u7edf\u8ba1\u7279\u5b9a\u5217\u4e0a\u7684\u4e0d\u91cd\u590d\u884c\u7684\u6570\u91cf\uff1a<\/strong><\/li>\n<\/ol>\n<pre>SELECT COUNT(DISTINCT column_name) FROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u7edf\u8ba1\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\u6570\uff1a<\/strong><\/li>\n<\/ol>\n<pre>SELECT COUNT(*) FROM table_name WHERE column_name = value;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u67e5\u627e\u8868 &#8220;customers&#8221; \u4e2d\u5ba2\u6237\u7684\u6570\u91cf\uff1a<\/p>\n<pre>SELECT COUNT(*) FROM customers;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u67e5\u627e\u8868 &#8220;orders&#8221; \u4e2d\u5177\u6709\u72b6\u6001\u4e3a &#8220;\u5df2\u5b8c\u6210&#8221; \u7684\u8ba2\u5355\u6570\u91cf\uff1a<\/p>\n<pre>SELECT COUNT(*) FROM orders WHERE status = 'Completed';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u67e5\u627e\u8868 &#8220;products&#8221; \u4e2d\u4e0d\u540c\u7c7b\u522b\u7684\u6570\u91cf\uff1a<\/p>\n<pre>SELECT COUNT(DISTINCT category) FROM products;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>COUNT() \u51fd\u6570\u5728\u8ba1\u7b97\u7a7a\u503c\u65f6\u8fd4\u56de 0\uff0c\u9664\u975e\u4f7f\u7528 COUNT(*)\uff0c\u5b83\u5c06\u8ba1\u6570\u6240\u6709\u884c\u3002<\/li>\n<li>COUNT(DISTINCT) \u51fd\u6570\u5ffd\u7565\u91cd\u590d\u503c\uff0c\u53ea\u8ba1\u7b97\u552f\u4e00\u503c\u3002<\/li>\n<li>COUNT() \u53ef\u4ee5\u4e0e\u5176\u4ed6\u805a\u5408\u51fd\u6570\uff08\u5982 SUM() \u6216 AVG()\uff09\u4e00\u8d77\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u4e2dcount\u51fd\u6570\u600e\u4e48\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>sql count() \u51fd\u6570\u7528\u4e8e\u7edf\u8ba1\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u884c\u6570\uff0c\u8bed\u6cd5\u4e3a count([distinct] column_name)\u3002\u53ef\u7528\u4e8e\u7edf\u8ba1\u8868\u4e2d\u6240\u6709\u884c\u7684\u6570\u91cf\u3001\u7279\u5b9a\u5217\u4e0a\u7684\u4e0d\u91cd\u590d\u884c\u7684\u6570\u91cf\u6216\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\u6570\u3002count() \u5728\u8ba1\u7b97\u7a7a\u503c\u65f6\u8fd4\u56de 0\uff0c\u9664\u975e\u4f7f\u7528 count(*)\uff0c\u5b83\u5c06\u8ba1\u6570\u6240\u6709\u884c\u3002count(distinct) \u51fd\u6570\u5ffd\u7565\u91cd\u590d\u503c\uff0c\u53ea\u8ba1\u7b97\u552f\u4e00\u503c\u3002\u53ef\u4ee5\u4e0e\u5176\u4ed6\u805a\u5408\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\u3002 SQL COUNT() \u51fd\u6570\u7684\u4f7f\u7528\u6307\u5357 SQL COUNT() \u51fd\u6570\u7528\u4e8e\u7edf\u8ba1\u8868\u4e2d\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u884c\u6570\u3002\u5b83\u662f\u4e00\u4e2a\uff0c\u5373\u5b83\u5c06\u591a\u4e2a\u503c\u7ec4\u5408\u4e3a\u4e00\u4e2a\u5355\u4e2a\u503c\u3002 \u8bed\u6cd5\uff1a COUNT([DISTINCT] column_name) \u767b\u5f55\u540e\u590d\u5236 \u53c2\u6570\uff1a DISTINCT (\u53ef\u9009)\uff1a \u6307\u5b9a\u53ea\u7edf\u8ba1\u552f\u4e00\u503c\u3002 column_name\uff1a \u8981\u7edf\u8ba1\u7684\u884c\u6216\u5217\u7684\u540d\u79f0\u3002 \u4f7f\u7528\u65b9\u6cd5\uff1a \u7edf\u8ba1\u8868\u4e2d\u6240\u6709\u884c\u7684\u6570\u91cf\uff1a SELECT COUNT(*) FROM table_name; \u767b\u5f55\u540e\u590d\u5236 \u7edf\u8ba1\u7279\u5b9a\u5217\u4e0a\u7684\u4e0d\u91cd\u590d\u884c\u7684\u6570\u91cf\uff1a SELECT COUNT(DISTINCT column_name) FROM table_name; \u767b\u5f55\u540e\u590d\u5236 \u7edf\u8ba1\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\u6570\uff1a SELECT COUNT(*) FROM table_name WHERE column_name = value; \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b\uff1a \u67e5\u627e\u8868 &#8220;customers&#8221; \u4e2d\u5ba2\u6237\u7684\u6570\u91cf\uff1a SELECT COUNT(*) [&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-5714","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5714","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=5714"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5714\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}