{"id":1772,"date":"2010-07-27T20:20:14","date_gmt":"2010-07-27T12:20:14","guid":{"rendered":"\/?p=1772"},"modified":"2010-07-27T20:20:14","modified_gmt":"2010-07-27T12:20:14","slug":"1772","status":"publish","type":"post","link":"https:\/\/www.justzz.com\/spec\/1772.html","title":{"rendered":"SQL Server \u7d22\u5f15\u4f18\u5316"},"content":{"rendered":"<div>\n<p><font face=\"\u5b8b\u4f53\">&nbsp;&nbsp;&nbsp; <strong>\u805a\u96c6\u7d22\u5f15<\/strong>\uff0c\u8868\u4e2d\u5b58\u50a8\u7684\u6570\u636e\u6309\u7167\u7d22\u5f15\u7684\u987a\u5e8f\u5b58\u50a8\uff0c\u68c0\u7d22\u6548\u7387\u6bd4\u666e\u901a\u7d22\u5f15\u9ad8\uff0c\u4f46\u5bf9\u6570\u636e\u65b0\u589e\/\u4fee\u6539\/\u5220\u9664\u7684\u5f71\u54cd\u6bd4\u8f83\u5927 <\/font><\/p>\n<p><font face=\"\u5b8b\u4f53\">&nbsp;&nbsp;&nbsp; <strong>\u975e\u805a\u96c6\u7d22\u5f15<\/strong>\uff0c\u4e0d\u5f71\u54cd\u8868\u4e2d\u7684\u6570\u636e\u5b58\u50a8\u987a\u5e8f\uff0c\u68c0\u7d22\u6548\u7387\u6bd4\u805a\u96c6\u7d22\u5f15\u4f4e\uff0c\u5bf9\u6570\u636e\u65b0\u589e\/\u4fee\u6539\/\u5220\u9664\u7684\u5f71\u54cd\u5f88\u5c0f <\/font><\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\"><strong>\u5982\u4f55\u8ba9\u4f60\u7684 SQL \u8fd0\u884c\u5f97\u66f4\u5feb<\/strong><\/font><\/p>\n<p><strong><\/strong>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\">&nbsp;&nbsp;&nbsp; \u4eba\u4eec\u5728\u4f7f\u7528 SQL \u65f6\u5f80\u5f80\u4f1a\u9677\u5165\u4e00\u4e2a\u8bef\u533a\uff0c\u5373\u592a\u5173\u6ce8\u4e8e\u6240\u5f97\u7684\u7ed3\u679c\u662f\u5426\u6b63\u786e\uff0c\u800c\u5ffd\u7565\u4e86\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u6cd5\u4e4b\u95f4\u53ef\u80fd\u5b58\u5728\u7684\u6027\u80fd\u5dee\u5f02\uff0c\u8fd9\u79cd\u6027\u80fd\u5dee\u5f02\u5728\u5927\u578b\u7684\u6216\u662f\u590d\u6742\u7684\u6570\u636e\u5e93\u73af\u5883\u4e2d\uff08\u5982\u8054\u673a\u4e8b\u52a1\u5904\u7406 OLTP \u6216\u51b3\u7b56\u652f\u6301\u7cfb\u7edf DSS \uff09\u4e2d\u8868\u73b0\u5f97\u5c24\u4e3a\u660e\u663e\u3002<br \/>&nbsp;&nbsp;&nbsp; \u7b14\u8005\u5728\u5de5\u4f5c\u5b9e\u8df5\u4e2d\u53d1\u73b0\uff0c\u4e0d\u826f\u7684 SQL \u5f80\u5f80\u6765\u81ea\u4e8e\u4e0d\u6070\u5f53\u7684\u7d22\u5f15\u8bbe\u8ba1\u3001\u4e0d\u5145\u4efd\u7684\u8fde\u63a5\u6761\u4ef6\u548c\u4e0d\u53ef\u4f18\u5316\u7684 where \u5b50\u53e5\u3002\u5728\u5bf9\u5b83\u4eec\u8fdb\u884c\u9002\u5f53\u7684\u4f18\u5316\u540e\uff0c\u5176\u8fd0\u884c\u901f\u5ea6\u6709\u4e86\u660e\u663e\u5730\u63d0\u9ad8\uff01<br \/>&nbsp;&nbsp;&nbsp; \u4e0b\u9762\u6211\u5c06\u4ece\u8fd9\u4e09\u4e2a\u65b9\u9762\u5206\u522b\u8fdb\u884c\u603b\u7ed3\u3002 <br \/>&nbsp;&nbsp;&nbsp; \u4e3a\u4e86\u66f4\u76f4\u89c2\u5730\u8bf4\u660e\u95ee\u9898\uff0c\u6240\u6709\u5b9e\u4f8b\u4e2d\u7684 SQL \u8fd0\u884c\u65f6\u95f4\u5747\u7ecf\u8fc7\u6d4b\u8bd5\uff0c\u4e0d\u8d85\u8fc7\uff11\u79d2\u7684\u5747 <br \/>\u8868\u793a\u4e3a\uff08 &lt; 1 \u79d2\uff09\u3002 <br \/>&nbsp;&nbsp;&nbsp; \u6d4b\u8bd5\u73af\u5883 --<br \/>&nbsp;&nbsp;&nbsp; \u4e3b\u673a\uff1a HP LH II<br \/>&nbsp;&nbsp;&nbsp; \u4e3b\u9891\uff1a 330MHZ<br \/>&nbsp;&nbsp;&nbsp; \u5185\u5b58\uff1a 128 \u5146 <br \/>&nbsp;&nbsp;&nbsp; \u64cd\u4f5c\u7cfb\u7edf\uff1a Operserver5.0.4<br \/>&nbsp;&nbsp;&nbsp; \u6570\u636e\u5e93\uff1a Sybase11.0.3<\/font><\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\"><strong>\u4e00\u3001\u4e0d\u5408\u7406\u7684\u7d22\u5f15\u8bbe\u8ba1<\/strong><\/font><\/p>\n<p><strong><\/strong>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\">\u4f8b\uff1a\u8868 record \u6709 620000 \u884c\uff0c\u8bd5\u770b\u5728\u4e0d\u540c\u7684\u7d22\u5f15\u4e0b\uff0c\u4e0b\u9762\u51e0\u4e2a SQL \u7684\u8fd0\u884c\u60c5\u51b5\uff1a<br \/>&nbsp;<br \/><strong>1. \u5728 date \u4e0a\u5efa\u6709\u4e00\u4e2a\u975e\u7fa4\u96c6\u7d22\u5f15<\/strong><\/font><\/p>\n<p>&nbsp;<\/p>\n<p><font color=\"#990099\" face=\"\u5b8b\u4f53\">select count(*) from record where date &gt; '19991201' and date &lt; '19991214' and amount &gt; 2000 (25\u79d2)<br \/>select date,sum(amount) from record group by date(55\u79d2)<br \/>select count(*) from record where date &gt; '19990901' and place in ('BJ','SH') (27\u79d2)<\/font><\/p>\n<p><font face=\"\u5b8b\u4f53\"><\/font>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\">\u5206\u6790\uff1a <br \/>&nbsp;&nbsp;&nbsp; date \u4e0a\u6709\u5927\u91cf\u7684\u91cd\u590d\u503c\uff0c\u5728\u975e\u7fa4\u96c6\u7d22\u5f15\u4e0b\uff0c\u6570\u636e\u5728\u7269\u7406\u4e0a\u968f\u673a\u5b58\u653e\u5728\u6570\u636e\u9875\u4e0a\uff0c\u5728 <br \/>\u8303\u56f4\u67e5\u627e\u65f6\uff0c\u5fc5\u987b\u6267\u884c\u4e00\u6b21\u8868\u626b\u63cf\u624d\u80fd\u627e\u5230\u8fd9\u4e00\u8303\u56f4\u5185\u7684\u5168\u90e8\u884c\u3002<\/font><\/p>\n<p><font face=\"\u5b8b\u4f53\">&nbsp;<\/font><\/p>\n<p><font face=\"\u5b8b\u4f53\"><strong>2. \u5728 date \u4e0a\u7684\u4e00\u4e2a\u7fa4\u96c6\u7d22\u5f15<\/strong><\/font><\/p>\n<p><font face=\"\u5b8b\u4f53\">&nbsp;<br \/><\/font><font color=\"#990099\">select count(*) from record where date &gt; '19991201' and date &lt; '19991214' and amount &gt; 2000 (14\u79d2)<br \/>select date,sum(amount) from record group by date(28\u79d2) <br \/>select count(*) from record where date &gt; '19990901' and place in ('BJ','SH') (14\u79d2)<\/font> <\/p>\n<p><font face=\"\u5b8b\u4f53\"><\/font>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\">\u5206\u6790\uff1a <br \/>&nbsp;&nbsp;&nbsp; \u5728\u7fa4\u96c6\u7d22\u5f15\u4e0b\uff0c\u6570\u636e\u5728\u7269\u7406\u4e0a\u6309\u987a\u5e8f\u5728\u6570\u636e\u9875\u4e0a\uff0c\u91cd\u590d\u503c\u4e5f\u6392\u5217\u5728\u4e00\u8d77\uff0c\u56e0\u800c\u5728\u8303\u56f4\u67e5\u627e\u65f6\uff0c\u53ef\u4ee5\u5148\u627e\u5230\u8fd9\u4e2a\u8303\u56f4\u7684\u8d77\u672b\u70b9\uff0c\u4e14\u53ea\u5728\u8fd9\u4e2a\u8303\u56f4\u5185\u626b\u63cf\u6570\u636e\u9875\uff0c\u907f\u514d\u4e86\u5927\u8303\u56f4\u626b\u63cf\uff0c\u63d0\u9ad8\u4e86\u67e5\u8be2\u901f\u5ea6\u3002 <\/font><\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\"><strong>3. \u5728 place \uff0c date \uff0c amount \u4e0a\u7684\u7ec4\u5408\u7d22\u5f15<\/strong><\/font><\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\"><\/font><font color=\"#990099\">select count(*) from record where date &gt; '19991201' and date &lt; '19991214' and amount &gt;2000 (26\u79d2) <br \/>select date,sum(amount) from record group by date(27\u79d2) <br \/>select count(*) from record where date &gt; '19990901' and place in ('BJ', 'SH') (&lt; 1 \u79d2)<\/font> <\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\">\u5206\u6790\uff1a<\/font><font face=\"\u5b8b\u4f53\">&nbsp;<br \/>&nbsp;&nbsp;&nbsp; \u8fd9\u662f\u4e00\u4e2a\u4e0d\u5f88\u5408\u7406\u7684\u7ec4\u5408\u7d22\u5f15\uff0c\u56e0\u4e3a\u5b83\u7684\u524d\u5bfc\u5217\u662f place \uff0c\u7b2c\u4e00\u548c\u7b2c\u4e8c\u6761 SQL \u6ca1\u6709\u5f15\u7528 place \uff0c\u56e0\u6b64\u4e5f\u6ca1\u6709\u5229\u7528\u4e0a\u7d22\u5f15\uff1b\u7b2c\u4e09\u4e2a SQL \u4f7f\u7528\u4e86 place\uff0c\u4e14\u5f15\u7528\u7684\u6240\u6709\u5217\u90fd\u5305\u542b\u5728\u7ec4\u5408\u7d22\u5f15\u4e2d\uff0c\u5f62\u6210\u4e86\u7d22\u5f15\u8986\u76d6\uff0c\u6240\u4ee5\u5b83\u7684\u901f\u5ea6\u662f\u975e\u5e38\u5feb\u7684\u3002<br \/>&nbsp;<br \/><strong>4. \u5728 date\uff0c place\uff0camount \u4e0a\u7684\u7ec4\u5408\u7d22\u5f15<\/strong><\/font><\/p>\n<p>&nbsp;<\/p>\n<p><font color=\"#990099\" face=\"\u5b8b\u4f53\">select count(*) from record where date &gt; '19991201' and date &lt; '19991214' and amount &gt;2000 (&lt;1\u79d2)<br \/>select date,sum(amount) from record group by date (11\u79d2) <br \/>select count(*) from record where date &gt; '19990901' and place in ('BJ','SH') (&lt;1\u79d2) <\/font><\/p>\n<p><font face=\"\u5b8b\u4f53\"><\/font>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\">\u5206\u6790\uff1a <br \/>&nbsp;&nbsp;&nbsp; \u8fd9\u662f\u4e00\u4e2a\u5408\u7406\u7684\u7ec4\u5408\u7d22\u5f15\u3002\u5b83\u5c06 date \u4f5c\u4e3a\u524d\u5bfc\u5217\uff0c\u4f7f\u6bcf\u4e2a SQL \u90fd\u53ef\u4ee5\u5229\u7528\u7d22\u5f15\uff0c\u5e76\u4e14\u5728\u7b2c\u4e00\u548c\u7b2c\u4e09\u4e2aSQL \u4e2d\u5f62\u6210\u4e86\u7d22\u5f15\u8986\u76d6\uff0c\u56e0\u800c\u6027\u80fd\u8fbe\u5230\u4e86\u6700\u4f18\u3002<br \/>&nbsp;<br \/><strong>5. \u603b\u7ed3\uff1a<\/strong><\/font><\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\">&nbsp;&nbsp;&nbsp; \u7f3a\u7701\u60c5\u51b5\u4e0b\u5efa\u7acb\u7684\u7d22\u5f15\u662f\u975e\u7fa4\u96c6\u7d22\u5f15\uff0c\u4f46\u6709\u65f6\u5b83\u5e76\u4e0d\u662f\u6700\u4f73\u7684\uff1b\u5408\u7406\u7684\u7d22\u5f15\u8bbe\u8ba1\u8981\u5efa\u7acb\u5728\u5bf9\u5404\u79cd\u67e5\u8be2\u7684\u5206\u6790\u548c\u9884\u6d4b\u4e0a\u3002\u4e00\u822c\u6765\u8bf4\uff1a <br \/>&nbsp;&nbsp;&nbsp; \u2460 \u6709\u5927\u91cf\u91cd\u590d\u503c\u3001\u4e14\u7ecf\u5e38\u6709\u8303\u56f4\u67e5\u8be2\uff08 between, &gt;,&lt; \uff0c &gt;=,&lt; = \uff09\u548c order by\u3001 group by \u53d1\u751f\u7684\u5217\uff0c\u53ef\u8003\u8651\u5efa\u7acb\u7fa4\u96c6\u7d22\u5f15\uff1b <br \/>&nbsp;&nbsp;&nbsp; \u2461 \u7ecf\u5e38\u540c\u65f6\u5b58\u53d6\u591a\u5217\uff0c\u4e14\u6bcf\u5217\u90fd\u542b\u6709\u91cd\u590d\u503c\u53ef\u8003\u8651\u5efa\u7acb\u7ec4\u5408\u7d22\u5f15\uff1b <br \/>&nbsp;&nbsp;&nbsp; \u2462 \u7ec4\u5408\u7d22\u5f15\u8981\u5c3d\u91cf\u4f7f\u5173\u952e\u67e5\u8be2\u5f62\u6210\u7d22\u5f15\u8986\u76d6\uff0c\u5176\u524d\u5bfc\u5217\u4e00\u5b9a\u662f\u4f7f\u7528\u6700\u9891\u7e41\u7684\u5217\u3002 <\/font><\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\"><strong>\u4e8c\u3001\u4e0d\u5145\u4efd\u7684\u8fde\u63a5\u6761\u4ef6\uff1a<\/strong><\/font><\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\">\u4f8b\uff1a\u8868 card \u67097896\u884c\uff0c\u5728card_no\u4e0a\u6709\u4e00\u4e2a\u975e\u805a\u96c6\u7d22\u5f15\uff0c\u8868account\u6709191122\u884c\uff0c\u5728account_no \u4e0a\u6709\u4e00\u4e2a\u975e\u805a\u96c6\u7d22\u5f15\uff0c\u8bd5\u770b\u5728\u4e0d\u540c\u7684\u8868\u8fde\u63a5\u6761\u4ef6\u4e0b\uff0c\u4e24\u4e2a SQL \u7684\u6267\u884c\u60c5\u51b5\uff1a <\/font><\/p>\n<p><font face=\"\u5b8b\u4f53\"><\/font><font color=\"#990099\">select sum(a.amount) from account a,card b where a.card_no = b.card_no (20\u79d2)<\/font> <\/p>\n<p><font face=\"\u5b8b\u4f53\">\u5c06 SQL \u6539\u4e3a\uff1a<\/font><\/p>\n<p><font face=\"\u5b8b\u4f53\"><\/font><font color=\"#990099\">select sum(a.amount) from account a,card b where a.card_no = b.card_no and a.account_no=b.account_no (&lt;1\u79d2)<\/font> <\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\">\u5206\u6790\uff1a <br \/>&nbsp;&nbsp;&nbsp; \u5728\u7b2c\u4e00\u4e2a\u8fde\u63a5\u6761\u4ef6\u4e0b\uff0c\u6700\u4f73\u67e5\u8be2\u65b9\u6848\u662f\u5c06 account \u4f5c\u5916\u5c42\u8868\uff0c card \u4f5c\u5185\u5c42\u8868\uff0c\u5229\u7528 card \u4e0a\u7684\u7d22\u5f15\uff0c\u5176 I\/O \u6b21\u6570\u53ef\u7531\u4ee5\u4e0b\u516c\u5f0f\u4f30\u7b97\u4e3a\uff1a <br \/>&nbsp;&nbsp;&nbsp; \u5916\u5c42\u8868 account \u4e0a\u7684 22541 \u9875 + \uff08\u5916\u5c42\u8868 account \u7684 191122 \u884c * \u5185\u5c42\u8868 card \u4e0a\u5bf9\u5e94\u5916\u5c42\u8868\u7b2c\u4e00\u884c\u6240\u8981\u67e5\u627e\u7684 3 \u9875\uff09 =595907 \u6b21 I\/O<br \/>&nbsp;&nbsp;&nbsp; \u5728\u7b2c\u4e8c\u4e2a\u8fde\u63a5\u6761\u4ef6\u4e0b\uff0c\u6700\u4f73\u67e5\u8be2\u65b9\u6848\u662f\u5c06 card \u4f5c\u5916\u5c42\u8868\uff0c account \u4f5c\u5185\u5c42\u8868\uff0c\u5229\u7528account \u4e0a\u7684\u7d22\u5f15\uff0c\u5176 I\/O \u6b21\u6570\u53ef\u7531\u4ee5\u4e0b\u516c\u5f0f\u4f30\u7b97\u4e3a\uff1a <br \/>&nbsp;&nbsp;&nbsp; \u5916\u5c42\u8868 card \u4e0a\u7684 1944 \u9875 + \uff08\u5916\u5c42\u8868 card \u7684 7896 \u884c * \u5185\u5c42\u8868 account \u4e0a\u5bf9\u5e94\u5916\u5c42\u8868\u6bcf\u4e00\u884c\u6240\u8981\u67e5\u627e\u7684 4 \u9875\uff09 = 33528 \u6b21 I\/O<br \/>&nbsp;&nbsp;&nbsp; \u53ef\u89c1\uff0c\u53ea\u6709\u5145\u4efd\u7684\u8fde\u63a5\u6761\u4ef6\uff0c\u771f\u6b63\u7684\u6700\u4f73\u65b9\u6848\u624d\u4f1a\u88ab\u6267\u884c\u3002 <\/font><\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\"><strong>\u603b\u7ed3\uff1a<\/strong><\/font><\/p>\n<p><strong><\/strong>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\">&nbsp;&nbsp;&nbsp; 1. \u591a\u8868\u64cd\u4f5c\u5728\u88ab\u5b9e\u9645\u6267\u884c\u524d\uff0c\u67e5\u8be2\u4f18\u5316\u5668\u4f1a\u6839\u636e\u8fde\u63a5\u6761\u4ef6\uff0c\u5217\u51fa\u51e0\u7ec4\u53ef\u80fd\u7684\u8fde\u63a5\u65b9\u6848\u5e76\u4ece\u4e2d\u627e\u51fa\u7cfb\u7edf\u5f00\u9500\u6700\u5c0f\u7684\u6700\u4f73\u65b9\u6848\u3002\u8fde\u63a5\u6761\u4ef6\u8981\u5145\u4efd\u8003\u8651\u5e26\u6709\u7d22\u5f15\u7684\u8868\u3001\u884c\u6570\u591a\u7684\u8868\uff1b\u5185\u5916\u8868\u7684\u9009\u62e9\u53ef\u7531\u516c\u5f0f\uff1a\u5916\u5c42\u8868\u4e2d\u7684\u5339\u914d\u884c\u6570 * \u5185\u5c42\u8868\u4e2d\u6bcf\u4e00\u6b21\u67e5\u627e\u7684\u6b21\u6570\u786e\u5b9a\uff0c\u4e58\u79ef\u6700\u5c0f\u4e3a\u6700\u4f73\u65b9\u6848\u3002 <br \/>&nbsp;&nbsp;&nbsp; 2. \u67e5\u770b\u6267\u884c\u65b9\u6848\u7684\u65b9\u6cd5 -- \u7528 set showplanon \uff0c\u6253\u5f00 showplan \u9009\u9879\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u8fde\u63a5\u987a\u5e8f\u3001\u4f7f\u7528\u4f55\u79cd\u7d22\u5f15\u7684\u4fe1\u606f\uff1b\u60f3\u770b\u66f4\u8be6\u7ec6\u7684\u4fe1\u606f\uff0c\u9700\u7528 sa \u89d2\u8272\u6267\u884c dbcc(3604,310,302) \u3002<\/font><\/p>\n<p><font face=\"\u5b8b\u4f53\">&nbsp;<\/font><\/p>\n<p><font face=\"\u5b8b\u4f53\"><strong>\u4e09\u3001\u4e0d\u53ef\u4f18\u5316\u7684 where \u5b50\u53e5<\/strong><\/font><\/p>\n<p><strong><\/strong>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\"><strong>1. \u4f8b\uff1a\u4e0b\u5217 SQL \u6761\u4ef6\u8bed\u53e5\u4e2d\u7684\u5217\u90fd\u5efa\u6709\u6070\u5f53\u7684\u7d22\u5f15\uff0c\u4f46\u6267\u884c\u901f\u5ea6\u5374\u975e\u5e38\u6162\uff1a<br \/><\/strong>&nbsp;<br \/><\/font><font color=\"#990099\">select * from record where substring(card_no,1,4)='5378' (13\u79d2)<br \/>select * from record where amount\/30&lt; 1000 (11\u79d2)<br \/>select * from record where convert(char(10),date,112)='19991201' (10\u79d2)<\/font> <\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\">\u5206\u6790\uff1a <br \/>&nbsp;&nbsp;&nbsp; where \u5b50\u53e5\u4e2d\u5bf9\u5217\u7684\u4efb\u4f55\u64cd\u4f5c\u7ed3\u679c\u90fd\u662f\u5728 SQL \u8fd0\u884c\u65f6\u9010\u5217\u8ba1\u7b97\u5f97\u5230\u7684\uff0c\u56e0\u6b64\u5b83\u4e0d\u5f97\u4e0d\u8fdb\u884c\u8868\u641c\u7d22\uff0c\u800c\u6ca1\u6709\u4f7f\u7528\u8be5\u5217\u4e0a\u9762\u7684\u7d22\u5f15\uff1b\u5982\u679c\u8fd9\u4e9b\u7ed3\u679c\u5728\u67e5\u8be2\u7f16\u8bd1\u65f6\u5c31\u80fd\u5f97\u5230\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u88ab SQL \u4f18\u5316\u5668\u4f18\u5316\uff0c\u4f7f\u7528\u7d22\u5f15\uff0c\u907f\u514d\u8868\u641c\u7d22\uff0c\u56e0\u6b64\u5c06 SQL \u91cd\u5199\u6210\u4e0b\u9762\u8fd9\u6837\uff1a<\/font><\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\"><\/font><font color=\"#990099\">select * from record where card_no like '5378%' (&lt;1\u79d2) <br \/>select * from record where amount &lt; 1000*30 (&lt;1\u79d2)<br \/>select * from record where date= '1999\/12\/01' (&lt;1\u79d2)<\/font> <\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\">\u4f60\u4f1a\u53d1\u73b0 SQL \u660e\u663e\u5feb\u8d77\u6765\uff01<\/font><\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\"><strong>2. \u4f8b\uff1a\u8868 stuff \u6709 200000 \u884c\uff0c id_no \u4e0a\u6709\u975e\u7fa4\u96c6\u7d22\u5f15\uff0c\u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a SQL \uff1a<\/strong><br \/>&nbsp;<br \/><\/font><font color=\"#990099\">select count(*) from stuff where id_no in('0','1') (23\u79d2)<\/font><\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\">\u5206\u6790\uff1a <br \/>&nbsp;&nbsp;&nbsp; where \u6761\u4ef6\u4e2d\u7684 'in' \u5728\u903b\u8f91\u4e0a\u76f8\u5f53\u4e8e 'or' \uff0c\u6240\u4ee5\u8bed\u6cd5\u5206\u6790\u5668\u4f1a\u5c06 in ('0','1') \u8f6c\u5316\u4e3a id_no ='0' or id_no='1' \u6765\u6267\u884c\u3002\u6211\u4eec\u671f\u671b\u5b83\u4f1a\u6839\u636e\u6bcf\u4e2a or \u5b50\u53e5\u5206\u522b\u67e5\u627e\uff0c\u518d\u5c06\u7ed3\u679c\u76f8\u52a0\uff0c\u8fd9\u6837\u53ef\u4ee5\u5229\u7528 id_no \u4e0a\u7684\u7d22\u5f15\uff1b\u4f46\u5b9e\u9645\u4e0a\uff08\u6839\u636e showplan \uff09 , \u5b83\u5374\u91c7\u7528\u4e86 \"OR \u7b56\u7565 \"\uff0c\u5373\u5148\u53d6\u51fa\u6ee1\u8db3\u6bcf\u4e2a or \u5b50\u53e5\u7684\u884c\uff0c\u5b58\u5165\u4e34\u65f6\u6570\u636e\u5e93\u7684\u5de5\u4f5c\u8868\u4e2d\uff0c\u518d\u5efa\u7acb\u552f\u4e00\u7d22\u5f15\u4ee5\u53bb\u6389\u91cd\u590d\u884c\uff0c\u6700\u540e\u4ece\u8fd9\u4e2a\u4e34\u65f6\u8868\u4e2d\u8ba1\u7b97\u7ed3\u679c\u3002\u56e0\u6b64\uff0c\u5b9e\u9645\u8fc7\u7a0b\u6ca1\u6709\u5229\u7528 id_no \u4e0a\u7d22\u5f15\uff0c\u5e76\u4e14\u5b8c\u6210\u65f6\u95f4\u8fd8\u8981\u53d7 tempdb \u6570\u636e\u5e93\u6027\u80fd\u7684\u5f71\u54cd\u3002 <br \/>&nbsp;&nbsp;&nbsp; \u5b9e\u8df5\u8bc1\u660e\uff0c\u8868\u7684\u884c\u6570\u8d8a\u591a\uff0c\u5de5\u4f5c\u8868\u7684\u6027\u80fd\u5c31\u8d8a\u5dee\uff0c\u5f53 stuff \u6709 620000 \u884c\u65f6\uff0c\u6267\u884c\u65f6\u95f4\u7adf\u8fbe\u5230 220 \u79d2\uff01\u8fd8\u4e0d\u5982\u5c06 or \u5b50\u53e5\u5206\u5f00\uff1a<br \/>&nbsp;<br \/><\/font><font color=\"#990099\">select count(*) from stuff where id_no='0'<br \/>select count(*) from stuff where id_no='1'<\/font><\/p>\n<p><font color=\"#990099\"><\/font>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\">&nbsp;&nbsp;&nbsp; \u5f97\u5230\u4e24\u4e2a\u7ed3\u679c\uff0c\u518d\u4f5c\u4e00\u6b21\u52a0\u6cd5\u5408\u7b97\u3002\u56e0\u4e3a\u6bcf\u53e5\u90fd\u4f7f\u7528\u4e86\u7d22\u5f15\uff0c\u6267\u884c\u65f6\u95f4\u53ea\u6709 3 \u79d2\uff0c\u5728 620000 \u884c\u4e0b\uff0c\u65f6\u95f4\u4e5f\u53ea\u6709 4 \u79d2\u3002\u6216\u8005\uff0c\u7528\u66f4\u597d\u7684\u65b9\u6cd5\uff0c\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u5b58\u50a8\u8fc7\u7a0b\uff1a<br \/>&nbsp;<br \/><\/font><font color=\"#990099\">create proc count_stuff as<br \/>declare @a int<br \/>declare @b int<br \/>declare @c int<br \/>declare @d char(10)<br \/>begin<br \/>select @a=count(*) from stuff where id_no='0'<br \/>select @b=count(*) from stuff where id_no='1'<br \/>end<br \/>select @c=@a+@b<br \/>select @d=convert(char(10),@c)<br \/>print @d<\/font><\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\">&nbsp;&nbsp;&nbsp; \u76f4\u63a5\u7b97\u51fa\u7ed3\u679c\uff0c\u6267\u884c\u65f6\u95f4\u540c\u4e0a\u9762\u4e00\u6837\u5feb\uff01<\/font><\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"\u5b8b\u4f53\"><strong>\u603b\u7ed3\uff1a<\/strong>&nbsp;<\/font><\/p>\n<p><font face=\"\u5b8b\u4f53\"><br \/>&nbsp;&nbsp;&nbsp; \u53ef\u89c1\uff0c\u6240\u8c13\u4f18\u5316\u5373 where \u5b50\u53e5\u5229\u7528\u4e86\u7d22\u5f15\uff0c\u4e0d\u53ef\u4f18\u5316\u5373\u53d1\u751f\u4e86\u8868\u626b\u63cf\u6216\u989d\u5916\u5f00\u9500\u3002 <\/font><\/p>\n<p><font face=\"\u5b8b\u4f53\">&nbsp;&nbsp;&nbsp; 1. \u4efb\u4f55\u5bf9\u5217\u7684\u64cd\u4f5c\u90fd\u5c06\u5bfc\u81f4\u8868\u626b\u63cf\uff0c\u5b83\u5305\u62ec\u6570\u636e\u5e93\u51fd\u6570\u3001\u8ba1\u7b97\u8868\u8fbe\u5f0f\u7b49\u7b49\uff0c\u67e5\u8be2\u65f6\u8981\u5c3d\u53ef\u80fd\u5c06\u64cd\u4f5c\u79fb\u81f3\u7b49\u53f7\u53f3\u8fb9\u3002 <br \/>&nbsp;&nbsp;&nbsp; 2.in\u3001 or \u5b50\u53e5\u5e38\u4f1a\u4f7f\u7528\u5de5\u4f5c\u8868\uff0c\u4f7f\u7d22\u5f15\u5931\u6548\uff1b\u5982\u679c\u4e0d\u4ea7\u751f\u5927\u91cf\u91cd\u590d\u503c\uff0c\u53ef\u4ee5\u8003\u8651\u628a\u5b50\u53e5\u62c6\u5f00\uff1b\u62c6\u5f00\u7684\u5b50\u53e5\u4e2d\u5e94\u8be5\u5305\u542b\u7d22\u5f15\u3002 <br \/>&nbsp;&nbsp;&nbsp; 3. \u8981\u5584\u4e8e\u4f7f\u7528\u5b58\u50a8\u8fc7\u7a0b\uff0c\u5b83\u4f7f SQL \u53d8\u5f97\u66f4\u52a0\u7075\u6d3b\u548c\u9ad8\u6548\u3002 <\/font><\/p>\n<p><font face=\"\u5b8b\u4f53\">&nbsp;&nbsp;&nbsp; \u4ece\u4ee5\u4e0a\u8fd9\u4e9b\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa\uff0c SQL \u4f18\u5316\u7684\u5b9e\u8d28\u5c31\u662f\u5728\u7ed3\u679c\u6b63\u786e\u7684\u524d\u63d0\u4e0b\uff0c\u7528\u4f18\u5316\u5668\u53ef\u4ee5\u8bc6\u522b\u7684\u8bed\u53e5\uff0cr\u4efd\u5229\u7528\u7d22\u5f15\uff0c\u51cf\u5c11\u8868\u626b\u63cf\u7684 I\/O \u6b21\u6570\uff0c\u5c3d\u91cf\u907f\u514d\u8868\u641c\u7d22\u7684\u53d1\u751f\u3002\u5176\u5b9e SQL \u7684\u6027\u80fd\u4f18\u5316\u662f\u4e00\u4e2a\u590d\u6742\u7684\u8fc7\u7a0b\uff0c\u4e0a\u8ff0\u8fd9\u4e9b\u53ea\u662f\u5728\u5e94\u7528\u5c42\u6b21\u7684\u4e00\u79cd\u4f53\u73b0\uff0c\u6df1\u5165\u7814\u7a76\u8fd8\u4f1a\u6d89\u53ca\u6570\u636e\u5e93\u5c42\u7684\u8d44\u6e90\u914d\u7f6e\u3001\u7f51\u7edc\u5c42\u7684\u6d41\u91cf\u63a7\u5236\u4ee5\u53ca\u64cd\u4f5c\u7cfb\u7edf\u5c42\u7684\u603b\u4f53\u8bbe\u8ba1\u3002<\/font><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;&nbsp;&nbsp; \u805a\u96c6\u7d22\u5f15\uff0c\u8868\u4e2d\u5b58\u50a8\u7684\u6570\u636e\u6309\u7167\u7d22\u5f15\u7684\u987a\u5e8f\u5b58\u50a8\uff0c\u68c0\u7d22\u6548\u7387\u6bd4\u666e\u901a\u7d22\u5f15\u9ad8\uff0c\u4f46\u5bf9\u6570\u636e\u65b0\u589e\/\u4fee\u6539\/\u5220\u9664\u7684\u5f71\u54cd\u6bd4\u8f83\u5927 &nbsp;&nbsp;&nbsp; \u975e\u805a\u96c6\u7d22\u5f15\uff0c\u4e0d\u5f71\u54cd\u8868\u4e2d\u7684\u6570\u636e\u5b58\u50a8\u987a\u5e8f\uff0c\u68c0\u7d22\u6548\u7387\u6bd4\u805a\u96c6\u7d22\u5f15\u4f4e\uff0c\u5bf9\u6570\u636e\u65b0\u589e&hellip;<\/p>\n","protected":false},"author":13,"featured_media":7704,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-1772","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-t"],"_links":{"self":[{"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/posts\/1772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/comments?post=1772"}],"version-history":[{"count":0,"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/posts\/1772\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/media\/7704"}],"wp:attachment":[{"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/media?parent=1772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/categories?post=1772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.justzz.com\/spec\/wp-json\/wp\/v2\/tags?post=1772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}