{"id":143801,"date":"2024-11-01T01:21:21","date_gmt":"2024-10-31T17:21:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/143801\/"},"modified":"2024-11-01T01:21:22","modified_gmt":"2024-10-31T17:21:22","slug":"sql-%e5%a4%9a%e8%a1%a8%e7%b5%b1%e8%a8%88%ef%bc%9a%e5%84%aa%e5%8c%96%e6%95%b8%e6%93%9a%e5%ba%ab%e8%a8%ad%e8%a8%88%e7%b6%93%e9%a9%97%e5%88%86%e4%ba%ab-sql-%e7%b5%b1%e8%a8%88%e5%a4%9a%e5%80%8b%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/143801\/","title":{"rendered":"SQL \u591a\u8868\u7d71\u8a08\uff1a\u512a\u5316\u6578\u64da\u5eab\u8a2d\u8a08\u7d93\u9a57\u5206\u4eab (sql \u7d71\u8a08\u591a\u500b\u8868\u6578\u64da\u5eab\u8a2d\u8a08)"},"content":{"rendered":"<h1 id=\"sql-%e5%a4%9a%e8%a1%a8%e7%b5%b1%e8%a8%88%ef%bc%9a%e5%84%aa%e5%8c%96%e6%95%b8%e6%93%9a%e5%ba%ab%e8%a8%ad%e8%a8%88%e7%b6%93%e9%a9%97%e5%88%86%e4%ba%ab-lCXuBmokip\">SQL \u591a\u8868\u7d71\u8a08\uff1a\u512a\u5316\u6578\u64da\u5eab\u8a2d\u8a08\u7d93\u9a57\u5206\u4eab<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u6578\u64da\u5eab\u7684\u8a2d\u8a08\u8207\u7ba1\u7406\u5c0d\u65bc\u4f01\u696d\u7684\u904b\u71df\u81f3\u95dc\u91cd\u8981\u3002\u5c24\u5176\u662f\u5728\u8655\u7406\u591a\u8868\u7d71\u8a08\u6642\uff0c\u5982\u4f55\u6709\u6548\u5730\u8a2d\u8a08\u6578\u64da\u5eab\u4ee5\u63d0\u9ad8\u67e5\u8a62\u6548\u7387\uff0c\u6210\u70ba\u4e86\u8a31\u591a\u958b\u767c\u8005\u548c\u6578\u64da\u5eab\u7ba1\u7406\u54e1\u9762\u81e8\u7684\u6311\u6230\u3002\u672c\u6587\u5c07\u5206\u4eab\u4e00\u4e9b\u512a\u5316\u6578\u64da\u5eab\u8a2d\u8a08\u7684\u7d93\u9a57\uff0c\u7279\u5225\u662f\u5728\u9032\u884c\u591a\u8868\u7d71\u8a08\u6642\u7684\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2 id=\"%e7%90%86%e8%a7%a3%e5%a4%9a%e8%a1%a8%e7%b5%b1%e8%a8%88%e7%9a%84%e9%9c%80%e6%b1%82-lCXuBmokip\">\u7406\u89e3\u591a\u8868\u7d71\u8a08\u7684\u9700\u6c42<\/h2>\n<p>\u591a\u8868\u7d71\u8a08\u901a\u5e38\u6d89\u53ca\u5230\u5f9e\u591a\u500b\u8868\u4e2d\u63d0\u53d6\u6578\u64da\uff0c\u4e26\u9032\u884c\u805a\u5408\u6216\u5206\u6790\u3002\u9019\u7a2e\u9700\u6c42\u5728\u8a31\u591a\u61c9\u7528\u5834\u666f\u4e2d\u90fd\u975e\u5e38\u5e38\u898b\uff0c\u4f8b\u5982\u96fb\u5b50\u5546\u52d9\u5e73\u53f0\u7684\u92b7\u552e\u5831\u544a\u3001\u793e\u4ea4\u5a92\u9ad4\u7684\u7528\u6236\u4e92\u52d5\u5206\u6790\u7b49\u3002\u70ba\u4e86\u6709\u6548\u5730\u9032\u884c\u591a\u8868\u7d71\u8a08\uff0c\u9996\u5148\u9700\u8981\u7406\u89e3\u6578\u64da\u4e4b\u9593\u7684\u95dc\u4fc2\uff0c\u4e26\u5408\u7406\u8a2d\u8a08\u6578\u64da\u5eab\u7d50\u69cb\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e8%a8%ad%e8%a8%88%e7%9a%84%e5%9f%ba%e6%9c%ac%e5%8e%9f%e5%89%87-lCXuBmokip\">\u6578\u64da\u5eab\u8a2d\u8a08\u7684\u57fa\u672c\u539f\u5247<\/h2>\n<ul>\n<li><strong>\u6b63\u898f\u5316<\/strong>\uff1a\u5728\u8a2d\u8a08\u6578\u64da\u5eab\u6642\uff0c\u61c9\u9075\u5faa\u6b63\u898f\u5316\u539f\u5247\uff0c\u5c07\u6578\u64da\u5206\u6563\u5230\u4e0d\u540c\u7684\u8868\u4e2d\uff0c\u4ee5\u6e1b\u5c11\u5197\u9918\u548c\u63d0\u9ad8\u6578\u64da\u4e00\u81f4\u6027\u3002<\/li>\n<li><strong>\u7d22\u5f15<\/strong>\uff1a\u70ba\u4e86\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\uff0c\u61c9\u5728\u7d93\u5e38\u7528\u65bc\u67e5\u8a62\u7684\u5217\u4e0a\u5efa\u7acb\u7d22\u5f15\u3002\u9019\u5c0d\u65bc\u591a\u8868\u806f\u63a5\u548c\u805a\u5408\u67e5\u8a62\u5c24\u70ba\u91cd\u8981\u3002<\/li>\n<li><strong>\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u985e\u578b<\/strong>\uff1a\u6839\u64da\u5be6\u969b\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u985e\u578b\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7bc0\u7701\u5b58\u5132\u7a7a\u9593\u4e26\u63d0\u9ad8\u67e5\u8a62\u6548\u7387\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a4%9a%e8%a1%a8%e8%81%af%e6%8e%a5%e7%9a%84%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-lCXuBmokip\">\u591a\u8868\u806f\u63a5\u7684\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u9032\u884c\u591a\u8868\u7d71\u8a08\u6642\uff0c\u806f\u63a5\u64cd\u4f5c\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5be6\u8e10\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8-inner-join-lCXuBmokip\">1. \u4f7f\u7528 INNER JOIN<\/h3>\n<p>\u7576\u9700\u8981\u5f9e\u591a\u500b\u8868\u4e2d\u63d0\u53d6\u5339\u914d\u7684\u6578\u64da\u6642\uff0c\u4f7f\u7528 <code>INNER JOIN<\/code> \u662f\u6700\u6709\u6548\u7684\u9078\u64c7\u3002\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u53ea\u8fd4\u56de\u5728\u6240\u6709\u8868\u4e2d\u90fd\u6709\u7684\u6578\u64da\u3002<\/p>\n<pre><code>SELECT a.column1, b.column2\nFROM table_a a\nINNER JOIN table_b b ON a.id = b.a_id;<\/code><\/pre>\n<h3 id=\"2-%e9%81%bf%e5%85%8d%e4%bd%bf%e7%94%a8-select-lCXuBmokip\">2. \u907f\u514d\u4f7f\u7528 SELECT *<\/h3>\n<p>\u5728\u67e5\u8a62\u6642\uff0c\u61c9\u907f\u514d\u4f7f\u7528 <code>SELECT *<\/code>\uff0c\u800c\u662f\u660e\u78ba\u6307\u5b9a\u9700\u8981\u7684\u5217\u3002\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u6578\u64da\u50b3\u8f38\u91cf\uff0c\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\u3002<\/p>\n<pre><code>SELECT a.column1, b.column2\nFROM table_a a\nINNER JOIN table_b b ON a.id = b.a_id;<\/code><\/pre>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8%e8%81%9a%e5%90%88%e5%87%bd%e6%95%b8-lCXuBmokip\">3. \u4f7f\u7528\u805a\u5408\u51fd\u6578<\/h3>\n<p>\u5728\u9032\u884c\u7d71\u8a08\u6642\uff0c\u805a\u5408\u51fd\u6578\u5982 <code>SUM()<\/code>\u3001<code>COUNT()<\/code> \u7b49\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u9019\u4e9b\u51fd\u6578\u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u5feb\u901f\u7372\u53d6\u6240\u9700\u7684\u7d71\u8a08\u6578\u64da\u3002<\/p>\n<pre><code>SELECT a.category, COUNT(b.id) AS total_sales\nFROM table_a a\nINNER JOIN table_b b ON a.id = b.a_id\nGROUP BY a.category;<\/code><\/pre>\n<h2 id=\"%e6%80%a7%e8%83%bd%e5%84%aa%e5%8c%96%e6%8a%80%e5%b7%a7-lCXuBmokip\">\u6027\u80fd\u512a\u5316\u6280\u5de7<\/h2>\n<p>\u5728\u9032\u884c\u591a\u8868\u7d71\u8a08\u6642\uff0c\u6027\u80fd\u512a\u5316\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u6027\u80fd\u512a\u5316\u6280\u5de7\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u8996\u5716<\/strong>\uff1a\u5982\u679c\u67d0\u4e9b\u67e5\u8a62\u7d93\u5e38\u88ab\u4f7f\u7528\uff0c\u53ef\u4ee5\u8003\u616e\u5275\u5efa\u8996\u5716\u4f86\u7c21\u5316\u67e5\u8a62\u904e\u7a0b\u3002<\/li>\n<li><strong>\u5206\u5340\u8868<\/strong>\uff1a\u5c0d\u65bc\u5927\u578b\u6578\u64da\u96c6\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u5206\u5340\u8868\u4f86\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\u3002<\/li>\n<li><strong>\u5b9a\u671f\u7dad\u8b77\u7d22\u5f15<\/strong>\uff1a\u96a8\u8457\u6578\u64da\u7684\u589e\u9577\uff0c\u5b9a\u671f\u91cd\u5efa\u548c\u66f4\u65b0\u7d22\u5f15\u53ef\u4ee5\u4fdd\u6301\u67e5\u8a62\u6027\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-lCXuBmokip\">\u7d50\u8ad6<\/h2>\n<p>\u5728\u9032\u884c SQL \u591a\u8868\u7d71\u8a08\u6642\uff0c\u5408\u7406\u7684\u6578\u64da\u5eab\u8a2d\u8a08\u548c\u512a\u5316\u7b56\u7565\u662f\u63d0\u9ad8\u67e5\u8a62\u6548\u7387\u7684\u95dc\u9375\u3002\u901a\u904e\u9075\u5faa\u6b63\u898f\u5316\u539f\u5247\u3001\u5efa\u7acb\u7d22\u5f15\u3001\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u985e\u578b\u4ee5\u53ca\u63a1\u7528\u6700\u4f73\u7684\u806f\u63a5\u548c\u805a\u5408\u65b9\u6cd5\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u5347\u6578\u64da\u8655\u7406\u7684\u6027\u80fd\u3002\u5e0c\u671b\u4ee5\u4e0a\u5206\u4eab\u7684\u7d93\u9a57\u80fd\u5c0d\u60a8\u5728\u6578\u64da\u5eab\u8a2d\u8a08\u548c\u7ba1\u7406\u4e0a\u6709\u6240\u5e6b\u52a9\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u6709\u95dc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u548c\u6578\u64da\u5eab\u7ba1\u7406\u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22SQL\u591a\u8868\u7d71\u8a08\u7684\u6700\u4f73\u5be6\u8e10\uff0c\u5206\u4eab\u512a\u5316\u6578\u64da\u5eab\u8a2d\u8a08\u7684\u7d93\u9a57\uff0c\u63d0\u5347\u67e5\u8a62\u6548\u7387\u8207\u6578\u64da\u7ba1\u7406\u80fd\u529b\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-143801","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/143801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=143801"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/143801\/revisions"}],"predecessor-version":[{"id":143802,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/143801\/revisions\/143802"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=143801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=143801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=143801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}