{"id":139147,"date":"2024-10-31T18:07:24","date_gmt":"2024-10-31T10:07:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/139147\/"},"modified":"2024-10-31T18:07:25","modified_gmt":"2024-10-31T10:07:25","slug":"sql-server%e6%95%b8%e6%93%9a%e6%9f%a5%e8%a9%a2%e5%9f%ba%e6%9c%ac%e6%96%b9%e6%b3%95%e7%9a%84%e6%8e%a2%e8%a8%8e","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/139147\/","title":{"rendered":"SQL Server\u6578\u64da\u67e5\u8a62\u57fa\u672c\u65b9\u6cd5\u7684\u63a2\u8a0e"},"content":{"rendered":"<h1 id=\"sql-server%e6%95%b8%e6%93%9a%e6%9f%a5%e8%a9%a2%e5%9f%ba%e6%9c%ac%e6%96%b9%e6%b3%95%e7%9a%84%e6%8e%a2%e8%a8%8e-FmmVtTkSpS\">SQL Server\u6578\u64da\u67e5\u8a62\u57fa\u672c\u65b9\u6cd5\u7684\u63a2\u8a0e<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0cSQL Server\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u696d\u52d9\u548c\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u638c\u63e1SQL Server\u7684\u6578\u64da\u67e5\u8a62\u57fa\u672c\u65b9\u6cd5\u5c0d\u65bc\u6578\u64da\u5206\u6790\u548c\u696d\u52d9\u6c7a\u7b56\u81f3\u95dc\u91cd\u8981\u3002\u672c\u6587\u5c07\u63a2\u8a0eSQL Server\u7684\u57fa\u672c\u67e5\u8a62\u65b9\u6cd5\uff0c\u5305\u62ecSELECT\u8a9e\u53e5\u3001WHERE\u5b50\u53e5\u3001JOIN\u64cd\u4f5c\u53ca\u805a\u5408\u51fd\u6578\u7b49\u3002<\/p>\n<h2 id=\"1-select%e8%aa%9e%e5%8f%a5-FmmVtTkSpS\">1. SELECT\u8a9e\u53e5<\/h2>\n<p>SELECT\u8a9e\u53e5\u662fSQL\u4e2d\u6700\u57fa\u672c\u7684\u67e5\u8a62\u8a9e\u53e5\uff0c\u7528\u65bc\u5f9e\u6578\u64da\u5eab\u4e2d\u6aa2\u7d22\u6578\u64da\u3002\u5176\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>SELECT column1, column2, ...\nFROM table_name;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u8981\u5f9e\u540d\u70ba\u201cCustomers\u201d\u7684\u8868\u4e2d\u9078\u64c7\u201cCustomerName\u201d\u548c\u201cCity\u201d\u5169\u500b\u5b57\u6bb5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT CustomerName, City\nFROM Customers;<\/code><\/pre>\n<h2 id=\"2-where%e5%ad%90%e5%8f%a5-FmmVtTkSpS\">2. WHERE\u5b50\u53e5<\/h2>\n<p>WHERE\u5b50\u53e5\u7528\u65bc\u904e\u6ffe\u67e5\u8a62\u7d50\u679c\uff0c\u50c5\u8fd4\u56de\u7b26\u5408\u7279\u5b9a\u689d\u4ef6\u7684\u8a18\u9304\u3002\u5176\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>SELECT column1, column2, ...\nFROM table_name\nWHERE condition;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u8981\u67e5\u8a62\u4f86\u81ea\u201cLondon\u201d\u7684\u5ba2\u6236\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT CustomerName, City\nFROM Customers\nWHERE City = 'London';<\/code><\/pre>\n<h2 id=\"3-join%e6%93%8d%e4%bd%9c-FmmVtTkSpS\">3. JOIN\u64cd\u4f5c<\/h2>\n<p>JOIN\u64cd\u4f5c\u7528\u65bc\u5f9e\u591a\u500b\u8868\u4e2d\u6aa2\u7d22\u6578\u64da\uff0c\u6839\u64da\u76f8\u95dc\u5b57\u6bb5\u5c07\u5b83\u5011\u9023\u63a5\u8d77\u4f86\u3002SQL Server\u652f\u6301\u591a\u7a2eJOIN\u985e\u578b\uff0c\u5305\u62ecINNER JOIN\u3001LEFT JOIN\u3001RIGHT JOIN\u548cFULL JOIN\u3002\u4ee5\u4e0b\u662fINNER JOIN\u7684\u57fa\u672c\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>SELECT columns\nFROM table1\nINNER JOIN table2\nON table1.common_field = table2.common_field;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u8981\u67e5\u8a62\u6bcf\u4f4d\u5ba2\u6236\u53ca\u5176\u8a02\u55ae\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT Customers.CustomerName, Orders.OrderID\nFROM Customers\nINNER JOIN Orders\nON Customers.CustomerID = Orders.CustomerID;<\/code><\/pre>\n<h2 id=\"4-%e8%81%9a%e5%90%88%e5%87%bd%e6%95%b8-FmmVtTkSpS\">4. \u805a\u5408\u51fd\u6578<\/h2>\n<p>\u805a\u5408\u51fd\u6578\u7528\u65bc\u8a08\u7b97\u4e00\u7d44\u503c\u7684\u7e3d\u548c\u3001\u5e73\u5747\u503c\u3001\u8a08\u6578\u7b49\u3002\u5e38\u7528\u7684\u805a\u5408\u51fd\u6578\u5305\u62ecSUM\u3001AVG\u3001COUNT\u3001MAX\u548cMIN\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u805a\u5408\u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>SELECT AGGREGATE_FUNCTION(column)\nFROM table_name;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u8981\u8a08\u7b97\u201cOrders\u201d\u8868\u4e2d\u6240\u6709\u8a02\u55ae\u7684\u7e3d\u91d1\u984d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT SUM(OrderAmount)\nFROM Orders;<\/code><\/pre>\n<h2 id=\"5-group-by%e5%92%8chaving%e5%ad%90%e5%8f%a5-FmmVtTkSpS\">5. GROUP BY\u548cHAVING\u5b50\u53e5<\/h2>\n<p>GROUP BY\u5b50\u53e5\u7528\u65bc\u5c07\u67e5\u8a62\u7d50\u679c\u6309\u6307\u5b9a\u5b57\u6bb5\u5206\u7d44\uff0c\u901a\u5e38\u8207\u805a\u5408\u51fd\u6578\u4e00\u8d77\u4f7f\u7528\u3002HAVING\u5b50\u53e5\u5247\u7528\u65bc\u904e\u6ffe\u5206\u7d44\u5f8c\u7684\u7d50\u679c\u3002\u5176\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>SELECT column1, AGGREGATE_FUNCTION(column2)\nFROM table_name\nGROUP BY column1\nHAVING condition;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u8981\u67e5\u8a62\u6bcf\u500b\u57ce\u5e02\u7684\u5ba2\u6236\u6578\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT City, COUNT(CustomerID)\nFROM Customers\nGROUP BY City\nHAVING COUNT(CustomerID) &gt; 5;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-FmmVtTkSpS\">\u7e3d\u7d50<\/h2>\n<p>\u638c\u63e1SQL Server\u7684\u57fa\u672c\u67e5\u8a62\u65b9\u6cd5\u5c0d\u65bc\u6578\u64da\u5206\u6790\u548c\u696d\u52d9\u6c7a\u7b56\u81f3\u95dc\u91cd\u8981\u3002\u901a\u904e\u4f7f\u7528SELECT\u8a9e\u53e5\u3001WHERE\u5b50\u53e5\u3001JOIN\u64cd\u4f5c\u53ca\u805a\u5408\u51fd\u6578\uff0c\u4f7f\u7528\u8005\u53ef\u4ee5\u6709\u6548\u5730\u5f9e\u6578\u64da\u5eab\u4e2d\u6aa2\u7d22\u548c\u5206\u6790\u6578\u64da\u3002\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u9577\uff0c\u5c0d\u65bc\u9ad8\u6548\u7684\u6578\u64da\u67e5\u8a62\u548c\u7ba1\u7406\u9700\u6c42\u4e5f\u65e5\u76ca\u589e\u52a0\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\u8b8a\u5f97\u5c24\u70ba\u91cd\u8981\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\u4ee5\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22SQL Server\u6578\u64da\u67e5\u8a62\u7684\u57fa\u672c\u65b9\u6cd5\uff0c\u5b78\u7fd2\u5982\u4f55\u6709\u6548\u5730\u63d0\u53d6\u548c\u7ba1\u7406\u6578\u64da\uff0c\u63d0\u5347\u6578\u64da\u5206\u6790\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-139147","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139147","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=139147"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139147\/revisions"}],"predecessor-version":[{"id":139148,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139147\/revisions\/139148"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=139147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=139147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=139147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}