{"id":168427,"date":"2024-11-06T12:17:25","date_gmt":"2024-11-06T04:17:25","guid":{"rendered":"https:\/\/server.hk\/cnblog\/168427\/"},"modified":"2024-11-06T12:17:26","modified_gmt":"2024-11-06T04:17:26","slug":"sql-%e5%84%aa%e5%8c%96%e9%80%99%e4%ba%94%e5%80%8b%e6%a5%b5%e7%b0%a1%e6%b3%95%e5%89%87%ef%bc%8c%e7%9b%b4%e6%8e%a5%e8%ae%93%e6%9f%a5%e8%a9%a2%e5%8e%9f%e5%9c%b0%e8%b5%b7%e9%a3%9b%ef%bc%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/168427\/","title":{"rendered":"SQL \u512a\u5316\u9019\u4e94\u500b\u6975\u7c21\u6cd5\u5247\uff0c\u76f4\u63a5\u8b93\u67e5\u8a62\u539f\u5730\u8d77\u98db\uff01"},"content":{"rendered":"<h1 id=\"sql-%e5%84%aa%e5%8c%96%e9%80%99%e4%ba%94%e5%80%8b%e6%a5%b5%e7%b0%a1%e6%b3%95%e5%89%87%ef%bc%8c%e7%9b%b4%e6%8e%a5%e8%ae%93%e6%9f%a5%e8%a9%a2%e5%8e%9f%e5%9c%b0%e8%b5%b7%e9%a3%9b%ef%bc%81-sGgWHcCBxo\">SQL \u512a\u5316\u9019\u4e94\u500b\u6975\u7c21\u6cd5\u5247\uff0c\u76f4\u63a5\u8b93\u67e5\u8a62\u539f\u5730\u8d77\u98db\uff01<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0cSQL\uff08\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff09\u662f\u8207\u6578\u64da\u5eab\u4e92\u52d5\u7684\u4e3b\u8981\u5de5\u5177\u3002\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u9577\uff0c\u67e5\u8a62\u6027\u80fd\u7684\u512a\u5316\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u4e94\u500b\u7c21\u55ae\u800c\u6709\u6548\u7684 SQL \u512a\u5316\u6cd5\u5247\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u63d0\u5347\u67e5\u8a62\u6548\u7387\uff0c\u8b93\u6578\u64da\u67e5\u8a62\u5982\u98db\u822c\u8fc5\u901f\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8%e7%b4%a2%e5%bc%95-sGgWHcCBxo\">1. \u4f7f\u7528\u7d22\u5f15<\/h2>\n<p>\u7d22\u5f15\u662f\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\u7684\u95dc\u9375\u3002\u5b83\u5011\u985e\u4f3c\u65bc\u66f8\u7c4d\u7684\u76ee\u9304\uff0c\u53ef\u4ee5\u5e6b\u52a9\u6578\u64da\u5eab\u5feb\u901f\u5b9a\u4f4d\u6240\u9700\u7684\u6578\u64da\u3002\u7576\u4f60\u5728\u4e00\u500b\u5927\u578b\u8868\u4e2d\u9032\u884c\u67e5\u8a62\u6642\uff0c\u6c92\u6709\u7d22\u5f15\u7684\u8a71\uff0c\u6578\u64da\u5eab\u9700\u8981\u6383\u63cf\u6574\u500b\u8868\uff0c\u9019\u6703\u5c0e\u81f4\u6027\u80fd\u4e0b\u964d\u3002<\/p>\n<pre><code>CREATE INDEX idx_column_name ON table_name(column_name);<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u70ba\u8868\u4e2d\u7684\u67d0\u4e00\u5217\u5275\u5efa\u4e86\u4e00\u500b\u7d22\u5f15\u3002\u9019\u6a23\uff0c\u7576\u6211\u5011\u67e5\u8a62\u8a72\u5217\u6642\uff0c\u6578\u64da\u5eab\u53ef\u4ee5\u66f4\u5feb\u5730\u627e\u5230\u6240\u9700\u7684\u6578\u64da\u3002<\/p>\n<h2 id=\"2-%e9%81%bf%e5%85%8d-select-sGgWHcCBxo\">2. \u907f\u514d SELECT *<\/h2>\n<p>\u4f7f\u7528 <code>SELECT *<\/code> \u6703\u5c0e\u81f4\u6578\u64da\u5eab\u8fd4\u56de\u8868\u4e2d\u7684\u6240\u6709\u5217\uff0c\u9019\u5728\u4e0d\u9700\u8981\u6240\u6709\u6578\u64da\u7684\u60c5\u6cc1\u4e0b\u6703\u6d6a\u8cbb\u8cc7\u6e90\u3002\u76f8\u53cd\uff0c\u61c9\u8a72\u660e\u78ba\u6307\u5b9a\u6240\u9700\u7684\u5217\u3002<\/p>\n<pre><code>SELECT column1, column2 FROM table_name WHERE condition;<\/code><\/pre>\n<p>\u9019\u6a23\u4e0d\u50c5\u53ef\u4ee5\u6e1b\u5c11\u50b3\u8f38\u7684\u6578\u64da\u91cf\uff0c\u9084\u80fd\u63d0\u9ad8\u67e5\u8a62\u7684\u57f7\u884c\u901f\u5ea6\u3002<\/p>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8-where-%e5%ad%90%e5%8f%a5%e9%81%8e%e6%bf%be%e6%95%b8%e6%93%9a-sGgWHcCBxo\">3. \u4f7f\u7528 WHERE \u5b50\u53e5\u904e\u6ffe\u6578\u64da<\/h2>\n<p>\u5728\u67e5\u8a62\u4e2d\u4f7f\u7528 <code>WHERE<\/code> \u5b50\u53e5\u53ef\u4ee5\u6709\u6548\u5730\u904e\u6ffe\u4e0d\u5fc5\u8981\u7684\u6578\u64da\uff0c\u5f9e\u800c\u6e1b\u5c11\u67e5\u8a62\u7684\u8ca0\u64d4\u3002\u9019\u6a23\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u67e5\u8a62\u7684\u6548\u7387\u3002<\/p>\n<pre><code>SELECT column1, column2 FROM table_name WHERE condition;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u7576\u4f60\u53ea\u9700\u8981\u7279\u5b9a\u689d\u4ef6\u4e0b\u7684\u6578\u64da\u6642\uff0c\u4f7f\u7528 <code>WHERE<\/code> \u5b50\u53e5\u53ef\u4ee5\u907f\u514d\u5168\u8868\u6383\u63cf\u3002<\/p>\n<h2 id=\"4-%e9%81%a9%e7%95%b6%e4%bd%bf%e7%94%a8-join-sGgWHcCBxo\">4. \u9069\u7576\u4f7f\u7528 JOIN<\/h2>\n<p>\u5728\u591a\u500b\u8868\u4e4b\u9593\u9032\u884c\u806f\u63a5\u6642\uff0c\u9078\u64c7\u5408\u9069\u7684\u806f\u63a5\u985e\u578b\uff08\u5982 INNER JOIN\u3001LEFT JOIN \u7b49\uff09\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u904e\u591a\u6216\u4e0d\u5fc5\u8981\u7684\u806f\u63a5\u6703\u5c0e\u81f4\u67e5\u8a62\u6027\u80fd\u4e0b\u964d\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<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86 INNER JOIN \u4f86\u7372\u53d6\u5169\u500b\u8868\u4e2d\u76f8\u95dc\u7684\u6578\u64da\uff0c\u9019\u6a23\u53ef\u4ee5\u6709\u6548\u5730\u6e1b\u5c11\u67e5\u8a62\u7684\u8907\u96dc\u6027\u3002<\/p>\n<h2 id=\"5-%e5%ae%9a%e6%9c%9f%e7%b6%ad%e8%ad%b7%e6%95%b8%e6%93%9a%e5%ba%ab-sGgWHcCBxo\">5. \u5b9a\u671f\u7dad\u8b77\u6578\u64da\u5eab<\/h2>\n<p>\u6578\u64da\u5eab\u7684\u6027\u80fd\u4e5f\u53d7\u5230\u5176\u7d50\u69cb\u548c\u72c0\u614b\u7684\u5f71\u97ff\u3002\u5b9a\u671f\u9032\u884c\u6578\u64da\u5eab\u7dad\u8b77\uff0c\u5982\u91cd\u5efa\u7d22\u5f15\u548c\u66f4\u65b0\u7d71\u8a08\u4fe1\u606f\uff0c\u53ef\u4ee5\u78ba\u4fdd\u67e5\u8a62\u6027\u80fd\u4fdd\u6301\u5728\u6700\u4f73\u72c0\u614b\u3002<\/p>\n<pre><code>-- \u91cd\u5efa\u7d22\u5f15\nALTER INDEX ALL ON table_name REBUILD;<\/code><\/pre>\n<p>\u9019\u6a23\u7684\u7dad\u8b77\u5de5\u4f5c\u53ef\u4ee5\u5e6b\u52a9\u6578\u64da\u5eab\u66f4\u6709\u6548\u5730\u4f7f\u7528\u7d22\u5f15\uff0c\u5f9e\u800c\u63d0\u9ad8\u67e5\u8a62\u901f\u5ea6\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-sGgWHcCBxo\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u4e94\u500b\u7c21\u55ae\u7684 SQL \u512a\u5316\u6cd5\u5247\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u986f\u8457\u63d0\u5347\u67e5\u8a62\u6027\u80fd\u3002\u7121\u8ad6\u662f\u4f7f\u7528\u7d22\u5f15\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5217\u9078\u64c7\uff0c\u9084\u662f\u9069\u7576\u4f7f\u7528\u806f\u63a5\u548c\u5b9a\u671f\u7dad\u8b77\u6578\u64da\u5eab\uff0c\u9019\u4e9b\u65b9\u6cd5\u90fd\u80fd\u5e6b\u52a9\u4f60\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\u4fdd\u6301\u9ad8\u6548\u3002<\/p>\n<p>\u5982\u679c\u4f60\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u7684\u670d\u52d9\u80fd\u5920\u6eff\u8db3\u4f60\u7684\u9700\u6c42\uff0c\u8b93\u4f60\u7684\u6578\u64da\u5eab\u904b\u884c\u66f4\u52a0\u6d41\u66a2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1\u9019\u4e94\u500bSQL\u512a\u5316\u6cd5\u5247\uff0c\u8b93\u4f60\u7684\u67e5\u8a62\u6548\u7387\u5927\u5e45\u63d0\u5347\uff0c\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u8655\u7406\u7684\u98db\u8e8d\uff01<\/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-168427","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168427","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=168427"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168427\/revisions"}],"predecessor-version":[{"id":168428,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168427\/revisions\/168428"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=168427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=168427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=168427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}