{"id":947,"date":"2025-01-15T00:24:46","date_gmt":"2025-01-14T16:24:46","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=947"},"modified":"2025-01-15T00:24:46","modified_gmt":"2025-01-14T16:24:46","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88-sql-server-%e4%b8%ad%e4%b8%8d%e8%83%bd%e5%9c%a8-where-%e5%ad%90%e5%8f%a5%e4%bd%bf%e7%94%a8%e7%aa%97%e5%8f%a3%e5%87%bd%e6%95%b0%ef%bc%9f%e8%af%a6%e8%a7%a3%e6%89%a7%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/15\/%e4%b8%ba%e4%bb%80%e4%b9%88-sql-server-%e4%b8%ad%e4%b8%8d%e8%83%bd%e5%9c%a8-where-%e5%ad%90%e5%8f%a5%e4%bd%bf%e7%94%a8%e7%aa%97%e5%8f%a3%e5%87%bd%e6%95%b0%ef%bc%9f%e8%af%a6%e8%a7%a3%e6%89%a7%e8%a1%8c\/","title":{"rendered":"\u4e3a\u4ec0\u4e48 SQL Server \u4e2d\u4e0d\u80fd\u5728 WHERE \u5b50\u53e5\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\uff1f\u8be6\u89e3\u6267\u884c\u987a\u5e8f\u4e0e\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 SQL Server \u4e2d\uff0c\u7a97\u53e3\u51fd\u6570\uff08\u5982 <code>ROW_NUMBER()<\/code>\u3001<code>RANK()<\/code>\u3001<code>DENSE_RANK()<\/code> \u7b49\uff09\u4e0d\u80fd\u76f4\u63a5\u5728 <code>WHERE<\/code> \u5b50\u53e5\u4e2d\u4f7f\u7528\uff0c\u4e3b\u8981\u662f\u56e0\u4e3a\u7a97\u53e3\u51fd\u6570\u7684\u6267\u884c\u987a\u5e8f\u548c SQL \u67e5\u8be2\u7684\u6574\u4f53\u6267\u884c\u6d41\u7a0b\u5b58\u5728\u51b2\u7a81\u3002\u4e3a\u4e86\u7406\u89e3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u6df1\u5165\u4e86\u89e3 SQL \u67e5\u8be2\u7684\u6267\u884c\u987a\u5e8f\u3001\u7a97\u53e3\u51fd\u6570\u7684\u7279\u70b9\u4ee5\u53ca\u5b83\u4eec\u5982\u4f55\u4e0e <code>WHERE<\/code> \u5b50\u53e5\u7684\u4f7f\u7528\u76f8\u4e92\u4f5c\u7528\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">1. SQL \u67e5\u8be2\u7684\u6267\u884c\u987a\u5e8f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">SQL \u67e5\u8be2\u7684\u6267\u884c\u987a\u5e8f\u5e76\u4e0d\u662f\u6309\u4e66\u5199\u987a\u5e8f\u8fdb\u884c\u7684\u3002\u4ee5\u4e0b\u662f\u5178\u578b\u7684 SQL \u67e5\u8be2\u6267\u884c\u987a\u5e8f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>FROM<\/strong> \u5b50\u53e5\uff1a\u9996\u5148\u786e\u5b9a\u6570\u636e\u6e90\uff0c\u5373\u9009\u62e9\u4ece\u54ea\u4e9b\u8868\u6216\u89c6\u56fe\u4e2d\u83b7\u53d6\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>ON<\/strong>\uff08\u5982\u679c\u6709\uff09\uff1a\u8fdb\u884c\u8fde\u63a5\u64cd\u4f5c\uff0c\u8bbe\u7f6e\u8054\u63a5\u6761\u4ef6\u3002<\/li>\n\n\n\n<li><strong>WHERE<\/strong> \u5b50\u53e5\uff1a\u8fc7\u6ee4\u884c\uff0c\u901a\u5e38\u662f\u5bf9\u8868\u4e2d\u7684\u539f\u59cb\u6570\u636e\u8fdb\u884c\u6761\u4ef6\u7b5b\u9009\u3002<\/li>\n\n\n\n<li><strong>GROUP BY<\/strong>\uff1a\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u3002<\/li>\n\n\n\n<li><strong>HAVING<\/strong>\uff1a\u5728\u5206\u7ec4\u540e\u5bf9\u6570\u636e\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u7b5b\u9009\u3002<\/li>\n\n\n\n<li><strong>SELECT<\/strong>\uff1a\u9009\u62e9\u6700\u7ec8\u7684\u5217\u3002<\/li>\n\n\n\n<li><strong>DISTINCT<\/strong>\uff1a\u53bb\u91cd\u3002<\/li>\n\n\n\n<li><strong>ORDER BY<\/strong>\uff1a\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n\n\n\n<li><strong>TOP<\/strong> \u6216 <strong>LIMIT<\/strong>\uff1a\u9650\u5236\u8fd4\u56de\u7684\u884c\u6570\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a97\u53e3\u51fd\u6570\u7684\u6267\u884c\u987a\u5e8f\u662f\u57fa\u4e8e <code>SELECT<\/code> \u5b50\u53e5\u4e4b\u540e\uff0c\u5b83\u4f1a\u5728\u6570\u636e\u96c6\u88ab\u8fd4\u56de\u4e4b\u524d\u5e94\u7528\u3002\u56e0\u6b64\uff0c\u7a97\u53e3\u51fd\u6570\u7684\u8ba1\u7b97\u662f\u5728 <code>WHERE<\/code> \u5b50\u53e5\u6267\u884c\u540e\u8fdb\u884c\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u7a97\u53e3\u51fd\u6570\u7684\u5b9a\u4e49\u4e0e\u5de5\u4f5c\u539f\u7406<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a97\u53e3\u51fd\u6570\u662f\u4e00\u79cd\u6309\u7279\u5b9a\u7684\u7a97\u53e3\uff08\u5373\u4e00\u7ec4\u4e0e\u5f53\u524d\u884c\u76f8\u5173\u7684\u884c\uff09\u8fdb\u884c\u8ba1\u7b97\u7684\u51fd\u6570\u3002\u5b83\u4eec\u5728\u7ed3\u679c\u96c6\u7684\u6bcf\u4e00\u884c\u4e0a\u6267\u884c\uff0c\u4e14\u8ba1\u7b97\u7ed3\u679c\u4f9d\u8d56\u4e8e\u524d\u540e\u884c\u7684\u503c\uff08\u4f8b\u5982\uff0c\u524d\u51e0\u884c\u3001\u540e\u51e0\u884c\u6216\u6574\u4e2a\u5206\u7ec4\u7684\u884c\uff09\u3002\u7a97\u53e3\u51fd\u6570\u7684\u7ed3\u679c\u5728 <strong>SELECT<\/strong> \u5b50\u53e5\u4e2d\u88ab\u8ba1\u7b97\uff0c\u5e76\u4e14\u901a\u5e38\u662f\u5728 <strong>ORDER BY<\/strong>\u3001<strong>PARTITION BY<\/strong> \u548c <strong>ROWS\/RANGE<\/strong> \u5b50\u53e5\u7684\u5e2e\u52a9\u4e0b\u5e94\u7528\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c<code>ROW_NUMBER()<\/code> \u7ed9\u6bcf\u4e2a\u5206\u7ec4\u4e2d\u7684\u884c\u5206\u914d\u4e00\u4e2a\u552f\u4e00\u7684\u7f16\u53f7\uff0c\u5176\u503c\u6839\u636e\u884c\u5728\u5206\u7ec4\u4e2d\u7684\u987a\u5e8f\u9012\u589e\u3002\u7a97\u53e3\u51fd\u6570\u8ba1\u7b97\u65f6\uff0c\u4f1a\u57fa\u4e8e\u4ece\u6570\u636e\u6e90\u4e2d\u68c0\u7d22\u51fa\u6765\u7684\u6240\u6709\u6570\u636e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4e0d\u80fd\u5728 WHERE \u5b50\u53e5\u4e2d\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u7684\u539f\u56e0<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>WHERE<\/code> \u5b50\u53e5\u4f5c\u7528\u4e8e\u539f\u59cb\u8868\u6570\u636e\u7684\u8fc7\u6ee4\uff0c\u5728\u6267\u884c\u987a\u5e8f\u4e0a\u65e9\u4e8e\u7a97\u53e3\u51fd\u6570\u7684\u8ba1\u7b97\uff0c\u56e0\u6b64\u5728 <code>WHERE<\/code> \u5b50\u53e5\u4e2d\u76f4\u63a5\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u4f1a\u5bfc\u81f4\u4ee5\u4e0b\u51e0\u4e2a\u95ee\u9898\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6267\u884c\u987a\u5e8f\u4e0d\u4e00\u81f4<\/strong>\uff1a<code>WHERE<\/code> \u5b50\u53e5\u5728\u67e5\u8be2\u5904\u7406\u7684\u65e9\u671f\u9636\u6bb5\u5c31\u5e94\u7528\uff0c\u56e0\u6b64\u5b83\u53ea\u80fd\u8bbf\u95ee\u67e5\u8be2\u4e2d\u7684\u57fa\u7840\u6570\u636e\u3002\u7a97\u53e3\u51fd\u6570\u9700\u8981\u5728 <code>SELECT<\/code> \u5b50\u53e5\u4e2d\u6267\u884c\uff0c\u5e76\u4e14\u5b83\u4eec\u7684\u8ba1\u7b97\u4f9d\u8d56\u4e8e\u6574\u4e2a\u6570\u636e\u96c6\uff0c\u56e0\u6b64 <code>WHERE<\/code> \u5b50\u53e5\u65e0\u6cd5\u8bc6\u522b\u6216\u4f7f\u7528\u8fd9\u4e9b\u7a97\u53e3\u51fd\u6570\u7684\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><strong>\u7a97\u53e3\u51fd\u6570\u4f9d\u8d56\u4e8e\u5176\u4ed6\u884c<\/strong>\uff1a\u7a97\u53e3\u51fd\u6570\u901a\u5e38\u4f1a\u6839\u636e\u67d0\u4e9b\u5206\u7ec4\u3001\u6392\u5e8f\u6216\u504f\u79fb\u7684\u7a97\u53e3\u8303\u56f4\u6765\u8ba1\u7b97\u5b83\u4eec\u7684\u503c\uff0c\u8fd9\u4e9b\u8ba1\u7b97\u901a\u5e38\u9700\u8981\u5728\u7ed3\u679c\u96c6\u7684\u57fa\u7840\u4e0a\u5b8c\u6210\u3002\u800c <code>WHERE<\/code> \u5b50\u53e5\u662f\u57fa\u4e8e\u539f\u59cb\u6570\u636e\u8fdb\u884c\u8fc7\u6ee4\uff0c\u5b83\u4e0d\u5177\u5907\u8de8\u884c\u8ba1\u7b97\u7684\u80fd\u529b\uff0c\u56e0\u6b64\u65e0\u6cd5\u5bf9\u7a97\u53e3\u51fd\u6570\u7684\u8f93\u51fa\u8fdb\u884c\u6761\u4ef6\u7b5b\u9009\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u89e3\u51b3\u65b9\u6cd5\uff1a\u4f7f\u7528 <code>HAVING<\/code> \u6216\u5b50\u67e5\u8be2<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u57fa\u4e8e\u7a97\u53e3\u51fd\u6570\u7684\u7ed3\u679c\u8fdb\u884c\u8fc7\u6ee4\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u6765\u89e3\u51b3\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u65b9\u6cd5 1: \u4f7f\u7528\u5b50\u67e5\u8be2<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c06\u7a97\u53e3\u51fd\u6570\u7684\u7ed3\u679c\u653e\u5230\u4e00\u4e2a\u5b50\u67e5\u8be2\u4e2d\uff0c\u7136\u540e\u5728\u5916\u90e8\u67e5\u8be2\u7684 <code>WHERE<\/code> \u5b50\u53e5\u4e2d\u8fdb\u884c\u7b5b\u9009\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT *\nFROM (\n    SELECT *, ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num\n    FROM employees\n) AS subquery\nWHERE row_num &lt;= 10;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>ROW_NUMBER()<\/code> \u5728\u5b50\u67e5\u8be2\u7684 <code>SELECT<\/code> \u4e2d\u8ba1\u7b97\uff0c\u7136\u540e\u5916\u90e8\u67e5\u8be2\u7684 <code>WHERE<\/code> \u5b50\u53e5\u7b5b\u9009\u51fa\u524d10\u540d\u5458\u5de5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u65b9\u6cd5 2: \u4f7f\u7528 <code>HAVING<\/code> \u5b50\u53e5<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>HAVING<\/code> \u5b50\u53e5\uff0c\u5b83\u4e0e <code>GROUP BY<\/code> \u914d\u5408\u4f7f\u7528\u6765\u5904\u7406\u5df2\u7ecf\u5206\u7ec4\u7684\u6570\u636e\u3002\u867d\u7136\u7a97\u53e3\u51fd\u6570\u672c\u8eab\u4e0d\u4f1a\u4e0e <code>GROUP BY<\/code> \u76f4\u63a5\u914d\u5408\uff0c\u4f46\u4f60\u53ef\u4ee5\u901a\u8fc7\u5148\u8ba1\u7b97\u7a97\u53e3\u51fd\u6570\uff0c\u518d\u4f7f\u7528 <code>HAVING<\/code> \u6765\u8fc7\u6ee4\u7ed3\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT *, ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num\nFROM employees\nHAVING ROW_NUMBER() OVER (ORDER BY salary DESC) &lt;= 10;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u65b9\u5f0f\u6709\u70b9\u7279\u6b8a\uff0c\u867d\u7136 SQL \u8bed\u6cd5\u5141\u8bb8\u5b83\uff0c\u4f46\u901a\u5e38\u63a8\u8350\u4f7f\u7528\u5b50\u67e5\u8be2\u65b9\u6cd5\uff0c\u56e0\u4e3a <code>HAVING<\/code> \u4e3b\u8981\u8bbe\u8ba1\u7528\u6765\u5904\u7406\u805a\u5408\u7ed3\u679c\uff0c\u800c\u7a97\u53e3\u51fd\u6570\u4e0e\u805a\u5408\u64cd\u4f5c\u7684\u6982\u5ff5\u4e0d\u540c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u80fd\u5728 SQL Server \u7684 <code>WHERE<\/code> \u5b50\u53e5\u4e2d\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\uff0c\u4e3b\u8981\u662f\u56e0\u4e3a\u7a97\u53e3\u51fd\u6570\u7684\u6267\u884c\u987a\u5e8f\u662f\u5728 <code>WHERE<\/code> \u8fc7\u6ee4\u4e4b\u540e\uff0c\u800c <code>WHERE<\/code> \u5b50\u53e5\u53ea\u80fd\u8fc7\u6ee4\u539f\u59cb\u6570\u636e\uff0c\u4e0d\u80fd\u5f15\u7528\u5728 <code>SELECT<\/code> \u5b50\u53e5\u4e2d\u540e\u7eed\u8ba1\u7b97\u7684\u7a97\u53e3\u51fd\u6570\u7684\u503c\u3002\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u7684\u65b9\u6cd5\u901a\u5e38\u662f\u4f7f\u7528\u5b50\u67e5\u8be2\uff0c\u6216\u8005\u6839\u636e\u5177\u4f53\u7684\u67e5\u8be2\u8981\u6c42\u4f7f\u7528 <code>HAVING<\/code> \u5b50\u53e5\u6765\u66ff\u4ee3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 SQL Server \u4e2d\uff0c\u7a97\u53e3\u51fd\u6570\uff08\u5982 ROW_NUMBER()\u3001RANK()\u3001DENSE_RANK()  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-947","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=947"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/947\/revisions"}],"predecessor-version":[{"id":948,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/947\/revisions\/948"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}