{"id":5178,"date":"2024-11-14T09:31:56","date_gmt":"2024-11-14T01:31:56","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5178\/"},"modified":"2024-11-14T09:31:56","modified_gmt":"2024-11-14T01:31:56","slug":"sql%e4%b8%adcount%e7%9a%84%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5178\/","title":{"rendered":"sql\u4e2dcount\u7684\u7528\u6cd5"},"content":{"rendered":"<blockquote><p>\n  sql \u4e2d count \u51fd\u6570\u7528\u4e8e\u7edf\u8ba1\u7279\u5b9a\u6761\u4ef6\u7684\u884c\u7684\u6570\u91cf\u3002\u57fa\u672c\u7528\u6cd5\u4e3a count(column_name)\uff0c\u7701\u7565 column_name \u7edf\u8ba1\u6240\u6709\u884c\uff0c\u6307\u5b9a column_name \u7edf\u8ba1\u975e\u7a7a\u884c\uff0cwhere \u5b50\u53e5\u53ef\u7528\u4e8e\u7edf\u8ba1\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\u3002distinct \u53ef\u7528\u4e8e\u53bb\u91cd\uff0cis null \u53ef\u7528\u4e8e\u7edf\u8ba1 null \u503c\u884c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/24\/2024052418362131873.jpg\" class=\"aligncenter\" title=\"sql\u4e2dcount\u7684\u7528\u6cd5\u63d2\u56fe\" alt=\"sql\u4e2dcount\u7684\u7528\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u4e2d COUNT \u7684\u7528\u6cd5<\/strong><\/p>\n<p>COUNT \u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u8868\u4e2d\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u884c\u7684\u6570\u91cf\u3002\u5176\u57fa\u672c\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<pre>COUNT(column_name)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff0ccolumn_name \u6307\u5b9a\u8981\u7edf\u8ba1\u7684\u884c\u3002<\/p>\n<p><strong>\u7528\u6cd5\u8be6\u89e3<\/strong><\/p>\n<ul>\n<li> <strong>\u7edf\u8ba1\u6240\u6709\u884c\uff1a<\/strong>\u5982\u679c\u7701\u7565 column_name\uff0c\u5219 COUNT \u51fd\u6570\u5c06\u7edf\u8ba1\u8868\u4e2d\u7684\u6240\u6709\u884c\u3002<\/li>\n<\/ul>\n<pre>SELECT COUNT(*) FROM table_name;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li> <strong>\u7edf\u8ba1\u7279\u5b9a\u5217\u7684\u884c\uff1a<\/strong>\u6307\u5b9a column_name \u4ee5\u7edf\u8ba1\u8be5\u5217\u4e2d\u5305\u542b\u975e\u7a7a\u503c\u7684\u884c\u7684\u6570\u91cf\u3002<\/li>\n<\/ul>\n<pre>SELECT COUNT(column_name) FROM table_name;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li> <strong>\u7edf\u8ba1\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\uff1a<\/strong>\u7ed3\u5408 WHERE \u5b50\u53e5\uff0c\u53ef\u4ee5\u7edf\u8ba1\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u884c\u3002<\/li>\n<\/ul>\n<pre>SELECT COUNT(*) FROM table_name WHERE column_name = 'value';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li> <strong>\u7edf\u8ba1\u53bb\u91cd\u884c\uff1a<\/strong>\u4f7f\u7528 DISTINCT \u5173\u952e\u5b57\u53ef\u4ee5\u7edf\u8ba1\u53bb\u91cd\u884c\u7684\u6570\u91cf\u3002\u4f8b\u5982\uff0c\u7edf\u8ba1\u4e0d\u540c\u4f5c\u8005\u7684\u6587\u7ae0\u6570\u91cf\uff1a<\/li>\n<\/ul>\n<pre>SELECT COUNT(DISTINCT author) FROM articles;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li> <strong>\u7edf\u8ba1 NULL \u503c\u7684\u884c\uff1a<\/strong>COUNT \u51fd\u6570\u4e0d\u7edf\u8ba1 NULL \u503c\u3002\u5982\u679c\u9700\u8981\u7edf\u8ba1 NULL \u503c\u7684\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528 COUNT(*) \u914d\u5408 IS NULL \u8fd0\u7b97\u7b26\u3002<\/li>\n<\/ul>\n<pre>SELECT COUNT(*) FROM table_name WHERE column_name IS NULL;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<ul>\n<li><strong>\u7edf\u8ba1\u8868\u4e2d\u6240\u6709\u884c\uff1a<\/strong><\/li>\n<\/ul>\n<pre>SELECT COUNT(*) FROM employees;\n-- \u8f93\u51fa\uff1a100<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li><strong>\u7edf\u8ba1\u7279\u5b9a\u5217\u4e2d\u975e\u7a7a\u503c\u7684\u884c\uff1a<\/strong><\/li>\n<\/ul>\n<pre>SELECT COUNT(salary) FROM employees;\n-- \u8f93\u51fa\uff1a95<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li><strong>\u7edf\u8ba1\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\uff1a<\/strong><\/li>\n<\/ul>\n<pre>SELECT COUNT(*) FROM employees WHERE department = 'Sales';\n-- \u8f93\u51fa\uff1a25<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li><strong>\u7edf\u8ba1\u53bb\u91cd\u884c\uff1a<\/strong><\/li>\n<\/ul>\n<pre>SELECT COUNT(DISTINCT name) FROM customers;\n-- \u8f93\u51fa\uff1a500<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li><strong>\u7edf\u8ba1 NULL \u503c\u7684\u884c\uff1a<\/strong><\/li>\n<\/ul>\n<pre>SELECT COUNT(*) FROM employees WHERE birthdate IS NULL;\n-- \u8f93\u51fa\uff1a5<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u4e2dcount\u7684\u7528\u6cd5\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 \u4e2d count \u51fd\u6570\u7528\u4e8e\u7edf\u8ba1\u7279\u5b9a\u6761\u4ef6\u7684\u884c\u7684\u6570\u91cf\u3002\u57fa\u672c\u7528\u6cd5\u4e3a count(column_name)\uff0c\u7701\u7565 column_name \u7edf\u8ba1\u6240\u6709\u884c\uff0c\u6307\u5b9a column_name \u7edf\u8ba1\u975e\u7a7a\u884c\uff0cwhere \u5b50\u53e5\u53ef\u7528\u4e8e\u7edf\u8ba1\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\u3002distinct \u53ef\u7528\u4e8e\u53bb\u91cd\uff0cis null \u53ef\u7528\u4e8e\u7edf\u8ba1 null \u503c\u884c\u3002 SQL \u4e2d COUNT \u7684\u7528\u6cd5 COUNT \u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u8868\u4e2d\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u884c\u7684\u6570\u91cf\u3002\u5176\u57fa\u672c\u8bed\u6cd5\u4e3a\uff1a COUNT(column_name) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0ccolumn_name \u6307\u5b9a\u8981\u7edf\u8ba1\u7684\u884c\u3002 \u7528\u6cd5\u8be6\u89e3 \u7edf\u8ba1\u6240\u6709\u884c\uff1a\u5982\u679c\u7701\u7565 column_name\uff0c\u5219 COUNT \u51fd\u6570\u5c06\u7edf\u8ba1\u8868\u4e2d\u7684\u6240\u6709\u884c\u3002 SELECT COUNT(*) FROM table_name; \u767b\u5f55\u540e\u590d\u5236 \u7edf\u8ba1\u7279\u5b9a\u5217\u7684\u884c\uff1a\u6307\u5b9a column_name \u4ee5\u7edf\u8ba1\u8be5\u5217\u4e2d\u5305\u542b\u975e\u7a7a\u503c\u7684\u884c\u7684\u6570\u91cf\u3002 SELECT COUNT(column_name) FROM table_name; \u767b\u5f55\u540e\u590d\u5236 \u7edf\u8ba1\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\uff1a\u7ed3\u5408 WHERE \u5b50\u53e5\uff0c\u53ef\u4ee5\u7edf\u8ba1\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u884c\u3002 SELECT COUNT(*) FROM table_name WHERE column_name = &#8216;value&#8217;; \u767b\u5f55\u540e\u590d\u5236 [&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-5178","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5178","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=5178"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5178\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}