{"id":195151,"date":"2024-11-13T12:38:20","date_gmt":"2024-11-13T04:38:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/195151\/"},"modified":"2024-11-13T12:38:21","modified_gmt":"2024-11-13T04:38:21","slug":"%e5%84%aa%e5%8c%96sql-select%e8%aa%9e%e5%8f%a5%e6%80%a7%e8%83%bd%e7%9a%846%e5%80%8b%e7%b0%a1%e5%96%ae%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/195151\/","title":{"rendered":"\u512a\u5316SQL SELECT\u8a9e\u53e5\u6027\u80fd\u76846\u500b\u7c21\u55ae\u6280\u5de7"},"content":{"rendered":"<h1 id=\"%e5%84%aa%e5%8c%96sql-select%e8%aa%9e%e5%8f%a5%e6%80%a7%e8%83%bd%e7%9a%846%e5%80%8b%e7%b0%a1%e5%96%ae%e6%8a%80%e5%b7%a7-rZkzcsGzOK\">\u512a\u5316SQL SELECT\u8a9e\u53e5\u6027\u80fd\u76846\u500b\u7c21\u55ae\u6280\u5de7<\/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\u9032\u884c\u4ea4\u4e92\u7684\u4e3b\u8981\u5de5\u5177\u4e4b\u4e00\u3002\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u9577\uff0c\u512a\u5316SQL\u67e5\u8a62\u7684\u6027\u80fd\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u516d\u500b\u7c21\u55ae\u7684\u6280\u5de7\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u512a\u5316SQL SELECT\u8a9e\u53e5\u7684\u6027\u80fd\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8%e7%b4%a2%e5%bc%95-rZkzcsGzOK\">1. \u4f7f\u7528\u7d22\u5f15<\/h2>\n<p>\u7d22\u5f15\u662f\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\u7684\u6700\u6709\u6548\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u904e\u5728\u8868\u7684\u67d0\u4e9b\u5217\u4e0a\u5275\u5efa\u7d22\u5f15\uff0c\u6578\u64da\u5eab\u53ef\u4ee5\u66f4\u5feb\u5730\u67e5\u627e\u548c\u6aa2\u7d22\u6578\u64da\u3002\u7576\u4f60\u57f7\u884cSELECT\u8a9e\u53e5\u6642\uff0c\u6578\u64da\u5eab\u6703\u5229\u7528\u7d22\u5f15\u4f86\u52a0\u901f\u67e5\u8a62\u904e\u7a0b\u3002<\/p>\n<pre><code>CREATE INDEX idx_column_name ON table_name(column_name);<\/code><\/pre>\n<p>\u7136\u800c\uff0c\u904e\u591a\u7684\u7d22\u5f15\u6703\u5f71\u97ffINSERT\u3001UPDATE\u548cDELETE\u64cd\u4f5c\u7684\u6027\u80fd\uff0c\u56e0\u6b64\u61c9\u6839\u64da\u5be6\u969b\u9700\u6c42\u8b39\u614e\u9078\u64c7\u7d22\u5f15\u3002<\/p>\n<h2 id=\"2-%e9%81%bf%e5%85%8dselect-rZkzcsGzOK\">2. \u907f\u514dSELECT *<\/h2>\n<p>\u4f7f\u7528SELECT *\u6703\u6aa2\u7d22\u8868\u4e2d\u7684\u6240\u6709\u5217\uff0c\u9019\u53ef\u80fd\u5c0e\u81f4\u4e0d\u5fc5\u8981\u7684\u6578\u64da\u50b3\u8f38\u548c\u8655\u7406\u3002\u76f8\u53cd\uff0c\u61c9\u8a72\u660e\u78ba\u6307\u5b9a\u6240\u9700\u7684\u5217\uff0c\u4ee5\u6e1b\u5c11\u6578\u64da\u91cf\u3002<\/p>\n<pre><code>SELECT column1, column2 FROM table_name WHERE condition;<\/code><\/pre>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8where%e5%ad%90%e5%8f%a5%e9%81%8e%e6%bf%be%e6%95%b8%e6%93%9a-rZkzcsGzOK\">3. \u4f7f\u7528WHERE\u5b50\u53e5\u904e\u6ffe\u6578\u64da<\/h2>\n<p>\u5728\u67e5\u8a62\u4e2d\u4f7f\u7528WHERE\u5b50\u53e5\u53ef\u4ee5\u6709\u6548\u5730\u904e\u6ffe\u4e0d\u5fc5\u8981\u7684\u6578\u64da\uff0c\u5f9e\u800c\u6e1b\u5c11\u67e5\u8a62\u7684\u7d50\u679c\u96c6\u5927\u5c0f\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u67e5\u8a62\u901f\u5ea6\uff0c\u9084\u6e1b\u5c11\u4e86\u6578\u64da\u5eab\u7684\u8ca0\u64d4\u3002<\/p>\n<pre><code>SELECT column1, column2 FROM table_name WHERE condition;<\/code><\/pre>\n<h2 id=\"4-%e9%81%a9%e7%95%b6%e4%bd%bf%e7%94%a8join-rZkzcsGzOK\">4. \u9069\u7576\u4f7f\u7528JOIN<\/h2>\n<p>\u5728\u591a\u500b\u8868\u4e4b\u9593\u9032\u884cJOIN\u64cd\u4f5c\u6642\uff0c\u61c9\u8a72\u9078\u64c7\u5408\u9069\u7684JOIN\u985e\u578b\uff08\u5982INNER JOIN\u3001LEFT JOIN\u7b49\uff09\uff0c\u4e26\u78ba\u4fddJOIN\u7684\u5217\u4e0a\u6709\u7d22\u5f15\u3002\u9019\u6a23\u53ef\u4ee5\u63d0\u9ad8\u67e5\u8a62\u7684\u6548\u7387\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 \nWHERE a.condition;<\/code><\/pre>\n<h2 id=\"5-%e4%bd%bf%e7%94%a8limit%e9%99%90%e5%88%b6%e7%b5%90%e6%9e%9c%e9%9b%86-rZkzcsGzOK\">5. \u4f7f\u7528LIMIT\u9650\u5236\u7d50\u679c\u96c6<\/h2>\n<p>\u5728\u9700\u8981\u6aa2\u7d22\u5927\u91cf\u6578\u64da\u6642\uff0c\u4f7f\u7528LIMIT\u5b50\u53e5\u53ef\u4ee5\u9650\u5236\u8fd4\u56de\u7684\u884c\u6578\uff0c\u5f9e\u800c\u63d0\u9ad8\u6027\u80fd\u3002\u9019\u5728\u8655\u7406\u5927\u6578\u64da\u96c6\u6642\u7279\u5225\u6709\u7528\u3002<\/p>\n<pre><code>SELECT column1, column2 FROM table_name WHERE condition LIMIT 10;<\/code><\/pre>\n<h2 id=\"6-%e5%ae%9a%e6%9c%9f%e5%88%86%e6%9e%90%e5%92%8c%e5%84%aa%e5%8c%96%e6%9f%a5%e8%a9%a2-rZkzcsGzOK\">6. \u5b9a\u671f\u5206\u6790\u548c\u512a\u5316\u67e5\u8a62<\/h2>\n<p>\u4f7f\u7528\u6578\u64da\u5eab\u63d0\u4f9b\u7684\u5206\u6790\u5de5\u5177\uff08\u5982EXPLAIN\uff09\u4f86\u6aa2\u67e5\u67e5\u8a62\u7684\u57f7\u884c\u8a08\u5283\uff0c\u627e\u51fa\u6027\u80fd\u74f6\u9838\u3002\u6839\u64da\u5206\u6790\u7d50\u679c\uff0c\u5c0d\u67e5\u8a62\u9032\u884c\u8abf\u6574\u548c\u512a\u5316\u3002<\/p>\n<pre><code>EXPLAIN SELECT column1, column2 FROM table_name WHERE condition;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-rZkzcsGzOK\">\u7e3d\u7d50<\/h2>\n<p>\u512a\u5316SQL SELECT\u8a9e\u53e5\u7684\u6027\u80fd\u662f\u63d0\u5347\u61c9\u7528\u7a0b\u5e8f\u6548\u7387\u7684\u95dc\u9375\u3002\u901a\u904e\u4f7f\u7528\u7d22\u5f15\u3001\u907f\u514dSELECT *\u3001\u5408\u7406\u4f7f\u7528WHERE\u5b50\u53e5\u548cJOIN\u3001\u9650\u5236\u7d50\u679c\u96c6\u4ee5\u53ca\u5b9a\u671f\u5206\u6790\u67e5\u8a62\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\u3002\u9019\u4e9b\u6280\u5de7\u4e0d\u50c5\u9069\u7528\u65bc\u5c0f\u578b\u6578\u64da\u5eab\uff0c\u4e5f\u5c0d\u5927\u578b\u6578\u64da\u5eab\u7684\u6027\u80fd\u512a\u5316\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u78ba\u4fdd\u5176\u5728\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u65b9\u9762\u9054\u5230\u6700\u4f73\u72c0\u614b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u512a\u5316SQL SELECT\u8a9e\u53e5\u6027\u80fd\u76846\u500b\u7c21\u55ae\u6280\u5de7\uff0c\u63d0\u5347\u67e5\u8a62\u6548\u7387\uff0c\u6e1b\u5c11\u57f7\u884c\u6642\u9593\uff0c\u8b93\u6578\u64da\u8655\u7406\u66f4\u6d41\u66a2\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-195151","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/195151","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=195151"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/195151\/revisions"}],"predecessor-version":[{"id":195152,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/195151\/revisions\/195152"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=195151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=195151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=195151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}