{"id":135155,"date":"2024-10-31T08:18:40","date_gmt":"2024-10-31T00:18:40","guid":{"rendered":"https:\/\/server.hk\/cnblog\/135155\/"},"modified":"2024-10-31T08:18:41","modified_gmt":"2024-10-31T00:18:41","slug":"sql-select%e8%aa%9e%e5%8f%a5%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/135155\/","title":{"rendered":"SQL SELECT\u8a9e\u53e5\u7684\u4f7f\u7528"},"content":{"rendered":"<h1 id=\"sql-select%e8%aa%9e%e5%8f%a5%e7%9a%84%e4%bd%bf%e7%94%a8-GrFqUPfYfw\">SQL SELECT\u8a9e\u53e5\u7684\u4f7f\u7528<\/h1>\n<p>\u5728\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0cSQL\uff08\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff09\u662f\u4e00\u7a2e\u7528\u65bc\u67e5\u8a62\u548c\u64cd\u4f5c\u8cc7\u6599\u7684\u6a19\u6e96\u8a9e\u8a00\u3002\u5176\u4e2d\uff0c<code>SELECT<\/code>\u8a9e\u53e5\u662f\u6700\u5e38\u7528\u7684\u8a9e\u53e5\u4e4b\u4e00\uff0c\u4e3b\u8981\u7528\u65bc\u5f9e\u8cc7\u6599\u8868\u4e2d\u6aa2\u7d22\u8cc7\u6599\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e<code>SELECT<\/code>\u8a9e\u53e5\u7684\u57fa\u672c\u7528\u6cd5\u3001\u5e38\u898b\u7684\u9078\u9805\u4ee5\u53ca\u4e00\u4e9b\u5be6\u7528\u7684\u7bc4\u4f8b\u3002<\/p>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e8%aa%9e%e6%b3%95-GrFqUPfYfw\">\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<p><code>SELECT<\/code>\u8a9e\u53e5\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>SELECT column1, column2, ...\nFROM table_name\nWHERE condition;<\/code><\/pre>\n<p>\u5728\u9019\u88e1\uff0c<code>column1, column2, ...<\/code>\u662f\u8981\u6aa2\u7d22\u7684\u6b04\u4f4d\u540d\u7a31\uff0c<code>table_name<\/code>\u662f\u8cc7\u6599\u8868\u7684\u540d\u7a31\uff0c\u800c<code>condition<\/code>\u5247\u662f\u7528\u65bc\u904e\u6ffe\u8cc7\u6599\u7684\u689d\u4ef6\u3002<\/p>\n<h2 id=\"%e9%81%b8%e6%93%87%e6%89%80%e6%9c%89%e6%ac%84%e4%bd%8d-GrFqUPfYfw\">\u9078\u64c7\u6240\u6709\u6b04\u4f4d<\/h2>\n<p>\u5982\u679c\u9700\u8981\u6aa2\u7d22\u8cc7\u6599\u8868\u4e2d\u7684\u6240\u6709\u6b04\u4f4d\uff0c\u53ef\u4ee5\u4f7f\u7528\u661f\u865f\uff08<code>*<\/code>\uff09\u4f86\u8868\u793a\uff1a<\/p>\n<pre><code>SELECT * FROM table_name;<\/code><\/pre>\n<p>\u9019\u6a23\u7684\u67e5\u8a62\u6703\u8fd4\u56de\u8cc7\u6599\u8868\u4e2d\u6240\u6709\u7684\u8cc7\u6599\u548c\u6b04\u4f4d\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8where%e5%ad%90%e5%8f%a5-GrFqUPfYfw\">\u4f7f\u7528WHERE\u5b50\u53e5<\/h2>\n<p>\u5728\u67e5\u8a62\u4e2d\uff0c<code>WHERE<\/code>\u5b50\u53e5\u7528\u65bc\u6307\u5b9a\u6aa2\u7d22\u8cc7\u6599\u7684\u689d\u4ef6\u3002\u4f8b\u5982\uff0c\u5047\u8a2d\u6709\u4e00\u500b\u540d\u70ba<code>employees<\/code>\u7684\u8cc7\u6599\u8868\uff0c\u5305\u542b\u54e1\u5de5\u7684\u59d3\u540d\u548c\u5e74\u9f61\uff0c\u82e5\u8981\u67e5\u8a62\u5e74\u9f61\u5927\u65bc30\u7684\u54e1\u5de5\uff0c\u53ef\u4ee5\u9019\u6a23\u5beb\uff1a<\/p>\n<pre><code>SELECT * FROM employees\nWHERE age &gt; 30;<\/code><\/pre>\n<h2 id=\"%e6%8e%92%e5%ba%8f%e7%b5%90%e6%9e%9c-GrFqUPfYfw\">\u6392\u5e8f\u7d50\u679c<\/h2>\n<p>\u4f7f\u7528<code>ORDER BY<\/code>\u5b50\u53e5\u53ef\u4ee5\u5c0d\u67e5\u8a62\u7d50\u679c\u9032\u884c\u6392\u5e8f\u3002\u4ee5\u4e0b\u7bc4\u4f8b\u5c07\u6839\u64da\u5e74\u9f61\u5c0d\u54e1\u5de5\u9032\u884c\u5347\u5e8f\u6392\u5e8f\uff1a<\/p>\n<pre><code>SELECT * FROM employees\nORDER BY age ASC;<\/code><\/pre>\n<p>\u82e5\u8981\u9032\u884c\u964d\u5e8f\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>DESC<\/code>\uff1a<\/p>\n<pre><code>SELECT * FROM employees\nORDER BY age DESC;<\/code><\/pre>\n<h2 id=\"%e9%99%90%e5%88%b6%e7%b5%90%e6%9e%9c%e6%95%b8%e9%87%8f-GrFqUPfYfw\">\u9650\u5236\u7d50\u679c\u6578\u91cf<\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u60a8\u53ef\u80fd\u53ea\u60f3\u6aa2\u7d22\u524d\u5e7e\u689d\u8a18\u9304\u3002\u53ef\u4ee5\u4f7f\u7528<code>LIMIT<\/code>\u5b50\u53e5\u4f86\u9650\u5236\u8fd4\u56de\u7684\u7d50\u679c\u6578\u91cf\uff1a<\/p>\n<pre><code>SELECT * FROM employees\nLIMIT 5;<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e8%81%9a%e5%90%88%e5%87%bd%e6%95%b8-GrFqUPfYfw\">\u4f7f\u7528\u805a\u5408\u51fd\u6578<\/h2>\n<p>SQL\u9084\u63d0\u4f9b\u4e86\u591a\u7a2e\u805a\u5408\u51fd\u6578\uff0c\u4f8b\u5982<code>COUNT<\/code>\u3001<code>SUM<\/code>\u3001<code>AVG<\/code>\u7b49\uff0c\u7528\u65bc\u8a08\u7b97\u8cc7\u6599\u7684\u7d71\u8a08\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u8a08\u7b97\u54e1\u5de5\u7684\u7e3d\u6578\uff0c\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre><code>SELECT COUNT(*) FROM employees;<\/code><\/pre>\n<h2 id=\"%e5%88%86%e7%b5%84%e6%9f%a5%e8%a9%a2-GrFqUPfYfw\">\u5206\u7d44\u67e5\u8a62<\/h2>\n<p>\u4f7f\u7528<code>GROUP BY<\/code>\u5b50\u53e5\u53ef\u4ee5\u5c07\u7d50\u679c\u96c6\u6839\u64da\u67d0\u500b\u6b04\u4f4d\u9032\u884c\u5206\u7d44\uff0c\u4e26\u8207\u805a\u5408\u51fd\u6578\u7d50\u5408\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u8a08\u7b97\u6bcf\u500b\u5e74\u9f61\u6bb5\u7684\u54e1\u5de5\u6578\u91cf\uff0c\u53ef\u4ee5\u9019\u6a23\u5beb\uff1a<\/p>\n<pre><code>SELECT age, COUNT(*) \nFROM employees\nGROUP BY age;<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e5%90%88%e5%a4%9a%e5%80%8b%e6%a2%9d%e4%bb%b6-GrFqUPfYfw\">\u7d50\u5408\u591a\u500b\u689d\u4ef6<\/h2>\n<p>\u5728<code>WHERE<\/code>\u5b50\u53e5\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u908f\u8f2f\u904b\u7b97\u7b26\uff08\u5982<code>AND<\/code>\u548c<code>OR<\/code>\uff09\u4f86\u7d50\u5408\u591a\u500b\u689d\u4ef6\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u67e5\u8a62\u5e74\u9f61\u5927\u65bc30\u4e14\u59d3\u540d\u4ee5\u201cJ\u201d\u958b\u982d\u7684\u54e1\u5de5\uff0c\u53ef\u4ee5\u9019\u6a23\u5beb\uff1a<\/p>\n<pre><code>SELECT * FROM employees\nWHERE age &gt; 30 AND name LIKE 'J%';<\/code><\/pre>\n<h2 id=\"%e5%b0%8f%e7%b5%90-GrFqUPfYfw\">\u5c0f\u7d50<\/h2>\n<p>SQL\u7684<code>SELECT<\/code>\u8a9e\u53e5\u662f\u8cc7\u6599\u6aa2\u7d22\u7684\u57fa\u790e\uff0c\u638c\u63e1\u5176\u7528\u6cd5\u5c0d\u65bc\u4efb\u4f55\u5f9e\u4e8b\u8cc7\u6599\u5eab\u7ba1\u7406\u6216\u5206\u6790\u7684\u4eba\u4f86\u8aaa\u90fd\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u900f\u904e\u9748\u6d3b\u904b\u7528\u5404\u7a2e\u5b50\u53e5\u548c\u51fd\u6578\uff0c\u4f7f\u7528\u8005\u53ef\u4ee5\u9ad8\u6548\u5730\u5f9e\u8cc7\u6599\u5eab\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u8cc7\u8a0a\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3\u5982\u4f55\u5728\u9999\u6e2f\u7684\u4f3a\u670d\u5668\u4e0a\u904b\u884cSQL\u67e5\u8a62\uff0c\u6216\u5c0b\u627e\u5408\u9069\u7684<code><a href=\"https:\/\/server.hk\">VPS<\/a><\/code>\u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3SQL SELECT\u8a9e\u53e5\u7684\u4f7f\u7528\uff0c\u638c\u63e1\u6578\u64da\u67e5\u8a62\u6280\u5de7\uff0c\u8f15\u9b06\u63d0\u53d6\u548c\u5206\u6790\u8cc7\u6599\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\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-135155","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135155","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=135155"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135155\/revisions"}],"predecessor-version":[{"id":135156,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135155\/revisions\/135156"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=135155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=135155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=135155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}