{"id":55,"date":"2012-12-13T17:53:44","date_gmt":"2012-12-13T09:53:44","guid":{"rendered":"http:\/\/0.19strokes.duapp.com\/?p=55"},"modified":"2024-10-23T21:26:20","modified_gmt":"2024-10-23T13:26:20","slug":"55","status":"publish","type":"post","link":"https:\/\/fengxinwei.com\/?p=55","title":{"rendered":"\u3010SQL\u3011SQL\u5206\u9875\u67e5\u8be2\u603b\u7ed3"},"content":{"rendered":"<p>\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7ecf\u5e38\u9047\u5230\u5206\u9875\u7684\u9700\u6c42\uff0c\u4eca\u5929\u5728\u6b64\u603b\u7ed3\u4e00\u4e0b\u5427\u3002<\/p>\n<p>\u7b80\u5355\u8bf4\u6765\u65b9\u6cd5\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u5728\u6e90\u4e0a\u63a7\u5236\uff0c\u4e00\u79cd\u5728\u7aef\u4e0a\u63a7\u5236\u3002\u6e90\u4e0a\u63a7\u5236\u628a\u5206\u9875\u903b\u8f91\u653e\u5728SQL\u5c42\uff1b\u7aef\u4e0a\u63a7\u5236\u4e00\u6b21\u6027\u83b7\u53d6\u6240\u6709\u6570\u636e\uff0c\u628a\u5206\u9875\u903b\u8f91\u653e\u5728UI\u4e0a\uff08\u5982GridView\uff09\u3002\u663e\u7136\uff0c\u7aef\u4e0a\u63a7\u5236\u5f00\u53d1\u96be\u5ea6\u4f4e\uff0c\u9002\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\uff0c\u4f46\u6570\u636e\u91cf\u589e\u5927\u65f6\u6027\u80fd\u548cIO\u6d88\u8017\u65e0\u6cd5\u63a5\u53d7\uff1b\u6e90\u4e0a\u63a7\u5236\u5728\u6027\u80fd\u548c\u5f00\u53d1\u96be\u5ea6\u4e0a\u8f83\u4e3a\u5e73\u8861\uff0c\u9002\u5e94\u5927\u591a\u6570\u4e1a\u52a1\u573a\u666f\uff1b\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u6839\u636e\u5ba2\u89c2\u60c5\u51b5\uff08\u6027\u80fd\u8981\u6c42\uff0c\u6e90\u4e0e\u7aef\u7684\u8d44\u6e90\u5360\u7528\u7b49\uff09\u5728\u6e90\u548c\u7aef\u4e4b\u95f4\u52a0\u4e00\u5c42\uff0c\u5e94\u7528\u7279\u6b8a\u7b97\u6cd5\u548c\u6280\u672f\u8fdb\u884c\u5904\u7406\u3002\u4ee5\u4e0b\u4e3b\u8981\u8ba8\u8bba\u6e90\u4e0a\uff0c\u5373SQL\u4e0a\u7684\u5206\u9875\u3002<\/p>\n<p>\u5206\u9875\u7684\u95ee\u9898\u5176\u5b9e\u5c31\u662f\u5728\u6ee1\u8db3\u6761\u4ef6\u7684\u4e00\u5806\u6709\u5e8f\u6570\u636e\u4e2d\u622a\u53d6\u5f53\u524d\u6240\u9700\u8981\u5c55\u793a\u7684\u90a3\u90e8\u5206\u3002\u5b9e\u9645\u4e0a\u5404\u79cd\u6570\u636e\u5e93\u90fd\u8003\u8651\u5230\u5206\u9875\u95ee\u9898\u800c\u5185\u7f6e\u4e86\u4e00\u4e9b\u7b56\u7565\uff0c\u6bd4\u5982MySql\u7684LIMIT\uff0cOracle\u7684ROWNUM\u548cROW_NUMBER()\uff0cSqlServer\u7684TOP\u548cROW_NUMBER()\uff0c\u57fa\u4e8e\u6b64\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4e00\u7cfb\u5217\u5206\u9875\u7684\u65b9\u6cd5\u3002<\/p>\n<p>1\u3001 \u57fa\u4e8eMySql\u7684LIMIT\u548cOracle\u7684ROWNUM\uff0c\u53ef\u4ee5\u76f4\u63a5\u9650\u5236\u8fd4\u56de\u533a\u95f4\uff08\u4ee5MySql\u4e3a\u4f8b\uff0c\u6ce8\u610f\u4f7f\u7528Oracle\u7684ROWNUM\u65f6\u8981\u5e94\u7528\u5b50\u67e5\u8be2\uff09\uff1a<\/p>\n<p><strong>\u65b9\u6cd5\u4e00\u3001\u76f4\u63a5\u9650\u5236\u8fd4\u56de\u533a\u95f4<\/strong><\/p>\n<p><code>SELECT * FROM table WHERE \u67e5\u8be2\u6761\u4ef6 ORDER BY \u6392\u5e8f\u6761\u4ef6 LIMIT ((\u9875\u7801-1)*\u9875\u5927\u5c0f),\u9875\u5927\u5c0f;<\/code><\/p>\n<p>\u4f18\u70b9\uff1a\u5199\u6cd5\u7b80\u5355\u3002<br \/>\n\u7f3a\u70b9\uff1a\u5f53\u9875\u7801\u548c\u9875\u5927\u5c0f\u8fc7\u5927\u65f6\uff0c\u6027\u80fd\u660e\u663e\u4e0b\u964d\u3002<br \/>\n\u9002\u7528\uff1a\u6570\u636e\u91cf\u4e0d\u5927\u3002<\/p>\n<p>2\u3001\u57fa\u4e8eLIMIT\uff08MySql\uff09\u3001ROWNUM\uff08Oracle\uff09\u548cTOP\uff08SqlServer\uff09\uff0c\u4ed6\u4eec\u53ef\u4ee5\u9650\u5236\u8fd4\u56de\u7684\u884c\u6570\uff0c\u56e0\u6b64\u53ef\u4ee5\u5f97\u5230\u4ee5\u4e0b\u4e24\u5957\u901a\u7528\u7684\u65b9\u6cd5\uff08\u4ee5SqlServer\u4e3a\u4f8b\uff09\uff1a<\/p>\n<p><strong>\u65b9\u6cd5\u4e8c\u3001NOT IN<\/strong><\/p>\n<p><code>SELECT TOP \u9875\u5927\u5c0f * FROM table WHERE \u4e3b\u952e NOT IN<br \/>\n(<br \/>\nSELECT TOP (\u9875\u7801-1)*\u9875\u5927\u5c0f \u4e3b\u952e FROM table WHERE \u67e5\u8be2\u6761\u4ef6 ORDER BY \u6392\u5e8f\u6761\u4ef6<br \/>\n)<br \/>\nORDER BY \u6392\u5e8f\u6761\u4ef6<\/code><\/p>\n<p>\u4f18\u70b9\uff1a\u901a\u7528\u6027\u5f3a\u3002<br \/>\n\u7f3a\u70b9\uff1a\u5f53\u6570\u636e\u91cf\u8f83\u5927\u65f6\u5411\u540e\u7ffb\u9875\uff0cNOT IN\u4e2d\u7684\u6570\u636e\u8fc7\u5927\u4f1a\u5f71\u54cd\u6027\u80fd\u3002<br \/>\n\u9002\u7528\uff1a\u6570\u636e\u91cf\u4e0d\u5927\u3002<\/p>\n<p><strong>\u65b9\u6cd5\u4e09\u3001MAX<\/strong><\/p>\n<p><code>SELECT TOP \u9875\u5927\u5c0f * FROM table WHERE \u67e5\u8be2\u6761\u4ef6 AND id &gt;<br \/>\n(<br \/>\nSELECT ISNULL(MAX(id),0) FROM<br \/>\n(<br \/>\nSELECT TOP ((\u9875\u7801-1)*\u9875\u5927\u5c0f) id FROM table WHERE \u67e5\u8be2\u6761\u4ef6 ORDER BY id<br \/>\n) AS tempTable<br \/>\n)<br \/>\nORDER BY id<\/code><\/p>\n<p>\u4f18\u70b9\uff1a\u901f\u5ea6\u5feb\uff0c\u7279\u522b\u662f\u5f53id\u4e3a\u4e3b\u952e\u65f6\u3002<br \/>\n\u7f3a\u70b9\uff1a\u9002\u7528\u9762\u7a84\uff0c\u8981\u6c42\u6392\u5e8f\u6761\u4ef6\u5355\u4e00\u4e14\u53ef\u6bd4\u8f83\u3002<br \/>\n\u9002\u7528\uff1a\u7b80\u5355\u6392\u5e8f\uff08\u7279\u6b8a\u60c5\u51b5\u4e5f\u53ef\u5c1d\u8bd5\u8f6c\u6362\u6210\u7c7b\u4f3c\u53ef\u6bd4\u8f83\u503c\u5904\u7406\uff09\u3002<\/p>\n<p>3\u3001\u57fa\u4e8eSqlServer\u548cOracle\u7684ROW_NUMBER()\uff0c\u53ef\u4ee5\u5f97\u5230\u8fd4\u56de\u6570\u636e\u7684\u884c\u53f7\uff0c\u57fa\u4e8e\u6b64\u5728\u9650\u5236\u8fd4\u56de\u533a\u95f4\u5f97\u5230\u5982\u4e0b\u65b9\u6cd5\uff08\u4ee5SqlServer\u4e3a\u4f8b\uff09\uff1a<\/p>\n<p><strong>\u65b9\u6cd5\u56db\u3001ROW_NUMBER()<\/strong><\/p>\n<p><code>SELECT TOP \u9875\u5927\u5c0f * FROM<br \/>\n(<br \/>\nSELECT TOP (\u9875\u7801*\u9875\u5927\u5c0f) ROW_NUMBER() OVER (ORDER BY \u6392\u5e8f\u6761\u4ef6) AS RowNum, * FROM table WHERE \u67e5\u8be2\u6761\u4ef6<br \/>\n) AS tempTable<br \/>\nWHERE RowNum BETWEEN (\u9875\u7801-1)*\u9875\u5927\u5c0f+1 AND \u9875\u7801*\u9875\u5927\u5c0f<br \/>\nORDER BY RowNum<\/code><\/p>\n<p>\u4f18\u70b9\uff1a\u5728\u6570\u636e\u91cf\u8f83\u5927\u65f6\u76f8\u6bd4NOT IN\u6709\u4f18\u52bf\u3002<br \/>\n\u7f3a\u70b9\uff1a\u5c0f\u6570\u636e\u91cf\u65f6\u4e0d\u5982NOT IN\u3002<br \/>\n\u9002\u7528\uff1a\u5927\u90e8\u5206\u5206\u9875\u67e5\u8be2\u9700\u6c42\u3002<\/p>\n<p>\u4ee5\u4e0a\u662f\u81ea\u5df1\u603b\u7ed3\u7684\u62d9\u89c1\uff0c\u6027\u80fd\u6bd4\u8f83\u6765\u81ea\u7f51\u4e0a\u8d44\u6599\u53ca\u4e2a\u4eba\u5224\u65ad\uff0c\u5e76\u6ca1\u6709\u6df1\u5165\u5b9e\u9a8c\uff0c\u4e0d\u5f53\u4e4b\u5904\u8bf7\u5927\u5bb6\u6307\u6b63\u3002<\/p>\n<p>\u535a\u5ba2\u56ed\uff1a<a title=\"\u3010SQL\u3011SQL\u5206\u9875\u67e5\u8be2\u603b\u7ed3\" href=\"http:\/\/www.cnblogs.com\/bbgasj\/archive\/2012\/11\/06\/2756567.html\" target=\"_blank\" rel=\"noopener\">\u3010SQL\u3011SQL\u5206\u9875\u67e5\u8be2\u603b\u7ed3<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7ecf\u5e38\u9047\u5230\u5206\u9875\u7684\u9700\u6c42\uff0c\u4eca\u5929\u5728\u6b64\u603b\u7ed3\u4e00\u4e0b\u5427\u3002 \u7b80\u5355\u8bf4\u6765\u65b9\u6cd5\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u5728\u6e90\u4e0a\u63a7\u5236\uff0c\u4e00\u79cd\u5728\u7aef\u4e0a\u63a7\u5236\u3002\u6e90\u4e0a\u63a7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[9,12],"class_list":["post-55","post","type-post","status-publish","format-standard","hentry","category-technology","tag-sql","tag-12"],"_links":{"self":[{"href":"https:\/\/fengxinwei.com\/index.php?rest_route=\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fengxinwei.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fengxinwei.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fengxinwei.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fengxinwei.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=55"}],"version-history":[{"count":1,"href":"https:\/\/fengxinwei.com\/index.php?rest_route=\/wp\/v2\/posts\/55\/revisions"}],"predecessor-version":[{"id":398,"href":"https:\/\/fengxinwei.com\/index.php?rest_route=\/wp\/v2\/posts\/55\/revisions\/398"}],"wp:attachment":[{"href":"https:\/\/fengxinwei.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fengxinwei.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fengxinwei.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}