{"id":80339,"date":"2024-10-16T14:11:17","date_gmt":"2024-10-16T06:11:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/80339\/"},"modified":"2024-10-16T14:11:17","modified_gmt":"2024-10-16T06:11:17","slug":"mysql%e4%b8%adselect%e5%91%bd%e4%bb%a4%e7%9a%84%e7%89%b9%e6%ae%8a%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/80339\/","title":{"rendered":"MySQL\u4e2dSELECT\u547d\u4ee4\u7684\u7279\u6b8a\u7528\u6cd5"},"content":{"rendered":"<h1 id=\"mysql%e4%b8%adselect%e5%91%bd%e4%bb%a4%e7%9a%84%e7%89%b9%e6%ae%8a%e7%94%a8%e6%b3%95-WLVhuTklKj\">MySQL\u4e2dSELECT\u547d\u4ee4\u7684\u7279\u6b8a\u7528\u6cd5<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\uff0cMySQL\u662f\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7cfb\u7d71\uff0c\u800cSELECT\u547d\u4ee4\u5247\u662f\u5176\u6700\u57fa\u672c\u4e14\u6700\u91cd\u8981\u7684\u64cd\u4f5c\u4e4b\u4e00\u3002SELECT\u547d\u4ee4\u4e0d\u50c5\u7528\u65bc\u67e5\u8a62\u6578\u64da\uff0c\u9084\u6709\u8a31\u591a\u7279\u6b8a\u7528\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u67e5\u8a62\u7684\u6548\u7387\u548c\u9748\u6d3b\u6027\u3002\u672c\u6587\u5c07\u63a2\u8a0eMySQL\u4e2dSELECT\u547d\u4ee4\u7684\u4e00\u4e9b\u7279\u6b8a\u7528\u6cd5\uff0c\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u61c9\u7528\u9019\u4e00\u547d\u4ee4\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8distinct%e9%97%9c%e9%8d%b5%e5%ad%97-WLVhuTklKj\">1. \u4f7f\u7528DISTINCT\u95dc\u9375\u5b57<\/h2>\n<p>\u5728\u67e5\u8a62\u4e2d\uff0c\u82e5\u5e0c\u671b\u8fd4\u56de\u552f\u4e00\u7684\u6578\u64da\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528DISTINCT\u95dc\u9375\u5b57\u3002\u9019\u5c0d\u65bc\u53bb\u9664\u91cd\u8907\u6578\u64da\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>SELECT DISTINCT column_name FROM table_name;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u6709\u4e00\u500b\u540d\u70ba\u201ccustomers\u201d\u7684\u8868\uff0c\u4e26\u5e0c\u671b\u67e5\u8a62\u6240\u6709\u4e0d\u540c\u7684\u57ce\u5e02\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>SELECT DISTINCT city FROM customers;<\/code><\/pre>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8limit%e9%99%90%e5%88%b6%e7%b5%90%e6%9e%9c%e9%9b%86-WLVhuTklKj\">2. \u4f7f\u7528LIMIT\u9650\u5236\u7d50\u679c\u96c6<\/h2>\n<p>\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\uff0c\u4f7f\u7528LIMIT\u95dc\u9375\u5b57\u53ef\u4ee5\u9650\u5236\u67e5\u8a62\u7d50\u679c\u7684\u6578\u91cf\uff0c\u9019\u5c0d\u65bc\u63d0\u9ad8\u6027\u80fd\u548c\u6e1b\u5c11\u6578\u64da\u50b3\u8f38\u91cf\u975e\u5e38\u6709\u5e6b\u52a9\u3002<\/p>\n<pre><code>SELECT * FROM table_name LIMIT 10;<\/code><\/pre>\n<p>\u9019\u689d\u547d\u4ee4\u5c07\u53ea\u8fd4\u56de\u524d10\u689d\u8a18\u9304\u3002\u82e5\u9700\u8981\u5f9e\u7b2c11\u689d\u958b\u59cb\u8fd4\u56de10\u689d\u8a18\u9304\uff0c\u53ef\u4ee5\u4f7f\u7528OFFSET\uff1a<\/p>\n<pre><code>SELECT * FROM table_name LIMIT 10 OFFSET 10;<\/code><\/pre>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8order-by%e6%8e%92%e5%ba%8f%e7%b5%90%e6%9e%9c-WLVhuTklKj\">3. \u4f7f\u7528ORDER BY\u6392\u5e8f\u7d50\u679c<\/h2>\n<p>ORDER BY\u5b50\u53e5\u53ef\u4ee5\u7528\u4f86\u5c0d\u67e5\u8a62\u7d50\u679c\u9032\u884c\u6392\u5e8f\u3002\u53ef\u4ee5\u6839\u64da\u4e00\u500b\u6216\u591a\u500b\u5217\u9032\u884c\u5347\u5e8f\u6216\u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n<pre><code>SELECT * FROM table_name ORDER BY column_name ASC;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u5e0c\u671b\u6309\u201ccreated_at\u201d\u5217\u7684\u964d\u5e8f\u6392\u5217\u201corders\u201d\u8868\u4e2d\u7684\u6578\u64da\uff0c\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre><code>SELECT * FROM orders ORDER BY created_at DESC;<\/code><\/pre>\n<h2 id=\"4-%e4%bd%bf%e7%94%a8group-by%e9%80%b2%e8%a1%8c%e5%88%86%e7%b5%84-WLVhuTklKj\">4. \u4f7f\u7528GROUP BY\u9032\u884c\u5206\u7d44<\/h2>\n<p>GROUP BY\u5b50\u53e5\u7528\u65bc\u5c07\u7d50\u679c\u96c6\u4e2d\u7684\u6578\u64da\u9032\u884c\u5206\u7d44\uff0c\u901a\u5e38\u8207\u805a\u5408\u51fd\u6578\uff08\u5982COUNT\u3001SUM\u3001AVG\u7b49\uff09\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<pre><code>SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u5e0c\u671b\u67e5\u8a62\u6bcf\u500b\u57ce\u5e02\u7684\u5ba2\u6236\u6578\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre><code>SELECT city, COUNT(*) FROM customers GROUP BY city;<\/code><\/pre>\n<h2 id=\"5-%e4%bd%bf%e7%94%a8join%e9%80%a3%e6%8e%a5%e5%a4%9a%e5%80%8b%e8%a1%a8-WLVhuTklKj\">5. \u4f7f\u7528JOIN\u9023\u63a5\u591a\u500b\u8868<\/h2>\n<p>\u5728\u6578\u64da\u5eab\u4e2d\uff0c\u6578\u64da\u901a\u5e38\u5206\u6563\u5728\u591a\u500b\u8868\u4e2d\u3002\u4f7f\u7528JOIN\u53ef\u4ee5\u5c07\u9019\u4e9b\u8868\u9023\u63a5\u8d77\u4f86\uff0c\u5f9e\u800c\u7372\u53d6\u66f4\u5168\u9762\u7684\u4fe1\u606f\u3002<\/p>\n<pre><code>SELECT a.column_name, b.column_name FROM table_a a JOIN table_b b ON a.common_column = b.common_column;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u5e0c\u671b\u67e5\u8a62\u6bcf\u500b\u8a02\u55ae\u53ca\u5176\u5c0d\u61c9\u7684\u5ba2\u6236\u540d\u7a31\uff0c\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre><code>SELECT orders.id, customers.name FROM orders JOIN customers ON orders.customer_id = customers.id;<\/code><\/pre>\n<h2 id=\"6-%e4%bd%bf%e7%94%a8%e5%ad%90%e6%9f%a5%e8%a9%a2-WLVhuTklKj\">6. \u4f7f\u7528\u5b50\u67e5\u8a62<\/h2>\n<p>\u5b50\u67e5\u8a62\u662f\u6307\u5728\u4e00\u500bSELECT\u8a9e\u53e5\u4e2d\u5d4c\u5957\u53e6\u4e00\u500bSELECT\u8a9e\u53e5\u3002\u9019\u53ef\u4ee5\u7528\u4f86\u9032\u884c\u66f4\u8907\u96dc\u7684\u67e5\u8a62\u3002<\/p>\n<pre><code>SELECT column_name FROM table_name WHERE column_name IN (SELECT column_name FROM another_table);<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u5e0c\u671b\u67e5\u8a62\u6240\u6709\u4f86\u81ea\u7279\u5b9a\u57ce\u5e02\u7684\u5ba2\u6236\uff0c\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre><code>SELECT * FROM customers WHERE city IN (SELECT city FROM special_cities);<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-WLVhuTklKj\">\u7e3d\u7d50<\/h2>\n<p>MySQL\u4e2d\u7684SELECT\u547d\u4ee4\u5177\u6709\u591a\u7a2e\u7279\u6b8a\u7528\u6cd5\uff0c\u9019\u4e9b\u7528\u6cd5\u4e0d\u50c5\u80fd\u63d0\u9ad8\u67e5\u8a62\u7684\u9748\u6d3b\u6027\uff0c\u9084\u80fd\u589e\u5f37\u6578\u64da\u8655\u7406\u7684\u6548\u7387\u3002\u7121\u8ad6\u662f\u4f7f\u7528DISTINCT\u53bb\u9664\u91cd\u8907\u6578\u64da\uff0c\u9084\u662f\u4f7f\u7528JOIN\u9023\u63a5\u591a\u500b\u8868\uff0c\u9019\u4e9b\u6280\u5de7\u90fd\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u7ba1\u7406\u548c\u67e5\u8a62\u6578\u64da\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u7528\u6236\uff0c\u9078\u64c7\u5408\u9069\u7684<a href=\"https:\/\/server.hk\">VPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u89e3\u6c7a\u65b9\u6848\u5c07\u662f\u660e\u667a\u4e4b\u8209\uff0c\u56e0\u70ba\u9019\u4e9b\u5e73\u53f0\u80fd\u63d0\u4f9b\u7a69\u5b9a\u7684\u6027\u80fd\u548c\u9748\u6d3b\u7684\u914d\u7f6e\u9078\u64c7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22MySQL\u4e2dSELECT\u547d\u4ee4\u7684\u7279\u6b8a\u7528\u6cd5\uff0c\u4e86\u89e3\u5982\u4f55\u9ad8\u6548\u67e5\u8be2\u6570\u636e\u3001\u4f7f\u7528\u5b50\u67e5\u8be2\u548c\u805a\u5408\u51fd\u6570\uff0c\u63d0\u5347\u6570\u636e\u5e93\u64cd\u4f5c\u6280\u80fd\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-80339","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/80339","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=80339"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/80339\/revisions"}],"predecessor-version":[{"id":80340,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/80339\/revisions\/80340"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=80339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=80339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=80339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}