{"id":136,"date":"2014-08-11T09:37:03","date_gmt":"2014-08-11T01:37:03","guid":{"rendered":"http:\/\/www.codeeel.com\/?p=136"},"modified":"2014-08-11T09:37:03","modified_gmt":"2014-08-11T01:37:03","slug":"sql-%e8%af%ad%e5%8f%a5%e6%80%a7%e8%83%bd%e8%b0%83%e4%bc%98","status":"publish","type":"post","link":"https:\/\/www.codeeel.com\/?p=136","title":{"rendered":"SQL \u8bed\u53e5\u6027\u80fd\u8c03\u4f18"},"content":{"rendered":"<p>\u6839\u636e\u6211\u4eec\u7684\u7ecf\u9a8c\uff08\u7531\u5f88\u591a\u4e1a\u754c\u4e13\u5bb6\u8bc1\u660e\uff09\uff0c\u5728 SQL Server \u4e0a\u53d6\u5f97\u7684\u6027\u80fd\u63d0\u9ad8\u6709 80% \u6765\u81ea\u5bf9 SQL \u7f16\u7801\u7684\u6539\u8fdb\uff0c\u800c\u4e0d\u662f\u6765\u81ea\u4e8e\u5bf9\u4e8e\u914d\u7f6e\u6216\u7cfb\u7edf\u6027\u80fd\u7684\u8c03\u6574\u3002\u201d<br \/>\n&#8212;\u51ef\u6587 \u514b\u83b1\u6069\u7b49\uff0cTransact-SQL Programming \u4f5c\u8005<\/p>\n<p>\u201c\u7ecf\u9a8c\u8868\u660e 80%-90% \u7684\u6027\u80fd\u8c03\u4f18\u662f\u5728\u5e94\u7528\u7ea7\u505a\u7684\uff0c\u800c\u4e0d\u662f\u5728\u6570\u636e\u5e93\u7ea7\u201d<br \/>\n&#8212;\u6258\u9a6c\u65af \u767d\u7279\uff0cExpert One on One: Oracle \u4f5c\u8005<\/p>\n<p>\u672c\u6587\u5c06\u4e3b\u8981\u8ba8\u8bba\u57fa\u4e8e\u8bed\u6cd5\u7684\u4f18\u5316\u4ee5\u53ca\u7b80\u7b80\u5355\u7684\u67e5\u8be2\u6761\u4ef6\u3002\u57fa\u4e8e\u8bed\u6cd5\u7684\u4f18\u5316\u6307\u7684\u662f\u4e3a\u4e0d\u8003\u8651\u4efb\u4f55\u7684\u975e\u8bed\u6cd5\u56e0\u7d20\uff08\u4f8b\u5982\uff0c\u7d22\u5f15\uff0c\u8868\u5927\u5c0f\u548c\u5b58\u50a8\u7b49\uff09\uff0c\u4ec5\u8003\u8651\u5728 SQL \u8bed\u53e5\u4e2d\u5bf9\u4e8e\u8bcd\u8bed\u7684\u9009\u62e9\u4ee5\u53ca\u4e66\u5199\u7684\u987a\u5e8f\u3002<\/p>\n<p>\u4e00\u822c\u89c4\u5219<\/p>\n<p>\u8fd9\u4e00\u90e8\u5206\uff0c\u5c06\u770b\u4e00\u4e0b\u4e00\u4e9b\u5728\u4e66\u5199\u7b80\u5355\u67e5\u8be2\u8bed\u65f6\u9700\u8981\u6ce8\u610f\u7684\u901a\u7528\u7684\u89c4\u5219\u3002<\/p>\n<p>\u6839\u636e\u6743\u503c\u6765\u4f18\u5316\u67e5\u8be2\u6761\u4ef6<\/p>\n<p>\u6700\u597d\u7684\u67e5\u8be2\u8bed\u53e5\u662f\u5c06\u7b80\u5355\u7684\u6bd4\u8f83\u64cd\u4f5c\u4f5c\u7528\u4e8e\u6700\u5c11\u7684\u884c\u4e0a\u3002\u4ee5\u4e0b\u4e24\u5f20\u8868\uff0c\u8868 1 \u548c\u8868 2 \u4ee5\u7531\u597d\u5230\u5dee\u7684\u987a\u5e8f\u5217\u51fa\u4e86\u5178\u578b\u67e5\u8be2\u6761\u4ef6\u64cd\u4f5c\u7b26\u5e76\u8d4b\u4e0e\u6743\u503c\u3002<\/p>\n<p>\u8868 1. \u67e5\u8be2\u6761\u4ef6\u4e2d\u64cd\u4f5c\u7b26\u7684\u6743\u503c<\/p>\n<p>\u64cd\u4f5c\u7b26\t \u6743\u503c<br \/>\n=\t 10<br \/>\n>\t 5<br \/>\n>=\t 5<br \/>\n<\t 5\n<=\t 5\nLIKE\t 3\n<>\t 0<br \/>\n\u8868 2. \u67e5\u8be2\u6761\u4ef6\u4e2d\u64cd\u4f5c\u6570\u7684\u6743\u503c<\/p>\n<p>\u64cd\u4f5c\u6570\t \u6743\u503c<br \/>\n\u4ec5\u5e38\u91cf\u5b57\u7b26\t 10<br \/>\n\u4ec5\u6709\u5217\u540d\t 5<br \/>\n\u4ec5\u6709\u53c2\u6570\t 5<br \/>\n\u591a\u64cd\u4f5c\u6570\u8868\u8fbe\u5f0f\t 3<br \/>\n\u7cbe\u786e\u6570\u503c\u7c7b\u578b\t 2<br \/>\n\u5176\u5b83\u6570\u503c\u7c7b\u578b\t 1<br \/>\n\u65f6\u95f4\u6570\u636e\u7c7b\u578b\t 1<br \/>\n\u5b57\u7b26\u6570\u636e\u7c7b\u578b\t 0<br \/>\nNULL\t 0<br \/>\n\u6839\u636e\u8868 1 \u548c\u8868 2 \u4e2d\u5206\u914d\u7684\u6743\u503c\uff0c\u53ef\u4ee5\u770b\u51fa\u6700\u597d\u7684\u67e5\u8be2\u6761\u4ef6\u5e94\u8be5\u662f\u50cf\u4e0b\u9762\u8fd9\u6837\u7684\uff1a<\/p>\n<p>\u2026 WHERE smallint_column = 789<br \/>\n\u8fd9\u4e2a\u4f8b\u5b50\u5f97\u5230 27 \u5206\uff0c\u8ba1\u7b97\u5982\u4e0b\uff1a<\/p>\n<p>\u5de6\u4fa7\u53ea\u6709\u5217\u540d\uff08smallint_column\uff09\u5f97 5 \u5206<br \/>\n\u64cd\u4f5c\u6570\u4e3a\u7cbe\u786e\u6570\u636e\u7c7b\u578b\uff08smallint_column\uff09\u5f97 2 \u5206<br \/>\n\u7b49\u53f7\uff08=\uff09\u64cd\u4f5c\u7b26\u5f97 10 \u5206<br \/>\n\u53f3\u4fa7\u662f\u6587\u5b57\u5b57\u7b26\uff08789\uff09\u5f97 10 \u5206<br \/>\n\u4e0b\u9762\u662f\u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50<\/p>\n<p>\u2026 WHERE char_column >= varchar_column \uff5c\uff5c \u2018 x \u2019<br \/>\n\u8fd9\u79cd\u7c7b\u578b\u7684\u67e5\u8be2\u6743\u503c\u5f97\u5206\u5c31\u5f88\u4f4e\uff0c\u53ea\u6709 13 \u5206<\/p>\n<p>\u5de6\u4fa7\u53ea\u6709\u5217\u540d\uff08char_column\uff09\u5f97 5 \u5206<br \/>\nCHAR \u7c7b\u578b\u7684\u64cd\u4f5c\u6570\u5f97 0 \u5206<br \/>\n\u5927\u4e8e\u7b49\u4e8e\u64cd\u4f5c\u7b26\u5f97 5 \u5206<br \/>\n\u5de6\u4fa7\u662f\u591a\u64cd\u4f5c\u6570\u8868\u8fbe\u793a\u5f97 3 \u5206<br \/>\nVARCHAR \u7c7b\u578b\u7684\u64cd\u4f5c\u6570\u5f97 0 \u5206<br \/>\n\u4e0a\u9762\u8868\u683c\u4e2d\u7684\u6743\u503c\u6570\u53ef\u80fd\u5728\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u4e2d\u4f1a\u6709\u6240\u4e0d\u540c\uff0c\u6240\u4ee5\u8bb0\u4f4f\u8fd9\u4e9b\u5177\u4f53\u6570\u503c\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u53ea\u9700\u8981\u4e86\u89e3\u5b83\u4eec\u7684\u6392\u5e8f\u5373\u53ef\u3002\u7528\u65f6\u8d8a\u5c11\u7684\u6bd4\u8f83\u6761\u4ef6\uff0c\u5f97\u5206\u4e5f\u5c31\u8d8a\u9ad8\uff0c\u8fd9\u6837\u7684\u6bd4\u8f83\u6761\u4ef6\u901a\u5e38\u662f\u90a3\u4e9b\u64cd\u4f5c\u7684\u884c\u6570\u5c11\u6216\u8005\u6613\u4e8e\u6bd4\u8f83\u7684\u3002<\/p>\n<p>\u4f20\u9012\u6cd5\u5219<\/p>\n<p>\u4f20\u9012\u6cd5\u5219\u662f\u8fd9\u6837\u5b9a\u4e49\u7684\uff1a<\/p>\n<p>IF\u3000(A <comparison operator> B) IS TRUE<br \/>\nAND (B <comparison operator> C) IS TRUE<br \/>\nTHEN (A <comparison operator> C) IS TRUE<br \/>\nAND NOT (A <comparison operator> C) IS FALSE<br \/>\n\u6bd4\u8f83\u8fd0\u7b97\u7b26\u5305\u62ec\uff1a=\u3001>\u3001>=\u3001<\u3001<\u3001+, \u4f46\u4e0d\u5305\u62ec\uff1a<>\u3001LIKE\u3002<\/p>\n<p>\u901a\u8fc7\u4f20\u9012\u6cd5\u5219\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0c\u53ef\u4ee5\u7528 C \u6765\u66ff\u6362 B\uff0c\u800c\u4e0d\u4f7f\u8868\u8fbe\u5f0f\u7684\u610f\u601d\u53d1\u751f\u53d8\u5316\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u4e24\u4e2a\u4f8b\u5b50\u8868\u8fbe\u4e86\u540c\u6837\u7684\u542b\u4e49\uff0c\u4f46\u662f\u7b2c\u4e8c\u4e2a\u8868\u8fbe\u793a\u8981\u6bd4\u7b2c\u4e00\u4e2a\u8868\u8fbe\u5f0f\u6267\u884c\u7684\u5feb\u3002<\/p>\n<p>\u8868\u8fbe\u5f0f\u4e00\uff1a<\/p>\n<p>&#8230; WHERE column1 < column2\nAND column2 = column3\n\nAND column1 = 5 |\n\n\u8868\u8fbe\u5f0f\u4e8c\uff1a\n\n... WHERE 5 < column2\nAND column2 = column3\n\nAND column1 = 5 |\n\n\u5927\u591a\u6570\u7684\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u90fd\u4f1a\u81ea\u52a8\u7684\u505a\u8fd9\u6837\u7684\u8c03\u6574\uff0c\u4f46\u662f\u5f53\u8868\u8fbe\u5f0f\u4e2d\u542b\u6709\u62ec\u53f7\u65f6\uff0c\u5b83\u4eec\u5c31\u4e0d\u4f1a\u81ea\u52a8\u8c03\u6574\u4e86\u3002\u4f8b\u5982\u4e00\u4e2a\u5982\u4e0b\u7684 SELECT \u8bed\u53e5\uff1a\n\nSELECT * FROM Table1\nWHERE column1 = 5 AND\n\nNOT (column3 = 7 OR column1 = column2) |\n\n\u5982\u679c\u8fdb\u884c\u8f6c\u5316\u7684\u8bdd\uff0c\u4f1a\u5f97\u5230\u5982\u4e0b\u7684\u8bed\u53e5\uff1a\n\nSELECT * FROM Table1\nWHERE column1 = 5\n\nAND column3 <> 7<\/p>\n<p>AND column2 <> 5 |<\/p>\n<p>\u8fdb\u884c\u8fd9\u6837\u53d8\u5316\u540e\u7684\u8bed\u53e5\u4f1a\u6bd4\u7b2c\u4e00\u4e2a\u6267\u884c\u7684\u66f4\u5feb\u3002<\/p>\n<p>1. Sargability<\/p>\n<p>\u7406\u60f3\u7684 SQL \u8868\u8fbe\u5f0f\u5e94\u8be5\u91c7\u7528\u4e0b\u9762\u8fd9\u79cd\u901a\u7528\u7684\u683c\u5f0f\uff1a<\/p>\n<column> <comparison operator> <literal>\n\u65e9\u4e9b\u65f6\u5019\uff0cIBM \u7814\u7a76\u4eba\u5458\u5c06\u8fd9\u79cd\u67e5\u8be2\u6761\u4ef6\u8bed\u540d\u547d\u540d\u4e3a\u201dsargable predicates\u201d\uff0c\u56e0\u4e3a SARG \u662f Search ARGument \u7684\u7ec4\u5408\u3002<\/p>\n<p>\u6839\u636e\u8fd9\u4e00\u89c4\u5219\uff0c\u67e5\u8be2\u6761\u4ef6\u7684\u5de6\u4fa7\u5e94\u8be5\u662f\u4e00\u4e2a\u5217\u540d\uff1b\u53f3\u4fa7\u5e94\u8be5\u662f\u4e00\u4e2a\u5f88\u5bb9\u6613\u8fdb\u884c\u67e5\u627e\u7684\u503c\u3002<\/p>\n<p>\u9075\u5faa\u8fd9\u4e00\u89c4\u5219\uff0c\u6240\u6709\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u90fd\u4f1a\u5c06\u5982\u4e0b\u7684\u8868\u8fbe\u5f0f\uff1a<\/p>\n<p>5 = column1<br \/>\n\u8f6c\u6362\u6210\uff1a<\/p>\n<p>column1 = 5<br \/>\n\u4f46\u662f\u5f53\u67e5\u8be2\u6761\u4ef6\u4e2d\u5305\u542b\u7b97\u672f\u8868\u8fbe\u5f0f\u65f6\uff0c\u53ea\u6709\u90e8\u5206\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<p>&#8230; WHERE column1 &#8211; 3 = -column2<br \/>\n\u8f6c\u6362\u6210\uff1a<\/p>\n<p>&#8230; WHERE column1 = -column2 + 3<br \/>\n\u8fd8\u662f\u53ef\u4ee5\u5e26\u6765\u67e5\u8be2\u6027\u80fd\u7684\u4f18\u5316\u7684\u3002<\/p>\n<p>\u9488\u5bf9\u4e13\u95e8\u64cd\u4f5c\u7b26\u7684\u8c03\u4f18<\/p>\n<p>\u524d\u9762\uff0c\u8bb2\u7684\u662f\u5173\u4e8e\u67e5\u8be2\u6761\u4ef6\u7684\u4e00\u822c\u89c4\u5219\uff0c\u5728\u8fd9\u4e00\u8282\u4e2d\uff0c\u5c06\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528\u4e13\u95e8\u7684\u64cd\u4f5c\u7b26\u6765\u6539\u8fdb SQL \u4ee3\u7801\u7684\u6027\u80fd\u3002<\/p>\n<p>\u4e0e (AND) <\/p>\n<p>\u6570\u636e\u5e93\u7cfb\u7edf\u6309\u7740\u4ece\u5de6\u5230\u53f3\u7684\u987a\u5e8f\u6765\u89e3\u6790\u4e00\u4e2a\u7cfb\u5217\u7531 AND \u8fde\u63a5\u7684\u8868\u8fbe\u5f0f\uff0c\u4f46\u662f Oracle \u5374\u662f\u4e2a\u4f8b\u5916\uff0c\u5b83\u662f\u4ece\u53f3\u5411\u5de6\u5730\u89e3\u6790\u8868\u8fbe\u5f0f\u3002\u53ef\u4ee5\u5229\u7528\u6570\u636e\u5e93\u7cfb\u7edf\u7684\u8fd9\u4e00\u7279\u6027\uff0c\u6765\u5c06\u6982\u7387\u5c0f\u7684\u8868\u8fbe\u793a\u653e\u5728\u524d\u9762\uff0c\u6216\u8005\u662f\u5982\u679c\u4e24\u4e2a\u8868\u8fbe\u5f0f\u53ef\u80fd\u6027\u76f8\u540c\uff0c\u90a3\u4e48\u53ef\u5c06\u76f8\u5bf9\u4e0d\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u653e\u5728\u524d\u9762\u3002\u8fd9\u6837\u505a\u7684\u8bdd\uff0c\u5982\u679c\u7b2c\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e3a\u5047\u7684\u8bdd\uff0c\u90a3\u4e48\u6570\u636e\u5e93\u7cfb\u7edf\u5c31\u4e0d\u5fc5\u518d\u8d39\u529b\u53bb\u89e3\u6790\u7b2c\u4e8c\u4e2a\u8868\u8fbe\u5f0f\u4e86\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8fd9\u6837\u8f6c\u6362\uff1a<\/p>\n<p>&#8230; WHERE column1 = &#8216;A&#8217; AND column2 = &#8216;B&#8217;<br \/>\n\u8f6c\u6362\u6210\uff1a<\/p>\n<p>&#8230; WHERE column2 = &#8216;B&#8217; AND column1 = &#8216;A&#8217;<br \/>\n\u8fd9\u91cc\u5047\u8bbe column2 = &#8216;B&#8217;\u7684\u6982\u7387\u8f83\u4f4e\uff0c\u5982\u679c\u662f Oracle \u6570\u636e\u5e93\u7684\u8bdd\uff0c\u53ea\u9700\u5c06\u89c4\u5219\u53cd\u8fc7\u6765\u7528\u5373\u53ef\u3002<\/p>\n<p>\u6216 (OR)<\/p>\n<p>\u548c\u4e0e (AND) \u64cd\u4f5c\u7b26\u76f8\u53cd\uff0c\u5728\u7528\u6216 (OR) \u64cd\u4f5c\u7b26\u5199 SQL \u8bed\u53e5\u65f6\uff0c\u5c31\u5e94\u8be5\u5c06\u6982\u7387\u5927\u7684\u8868\u8fbe\u793a\u653e\u5728\u5de6\u9762\uff0c\u56e0\u4e3a\u5982\u679c\u7b2c\u4e00\u4e2a\u8868\u8fbe\u793a\u4e3a\u5047\u7684\u8bdd\uff0cOR \u64cd\u4f5c\u7b26\u610f\u5473\u7740\u9700\u8981\u8fdb\u884c\u4e0b\u4e00\u4e2a\u8868\u8fbe\u793a\u7684\u89e3\u6790\u3002<\/p>\n<p>\u4e0e + \u6216<\/p>\n<p>\u6309\u7167\u96c6\u5408\u7684\u5c55\u5f00\u6cd5\u5219\uff0c<\/p>\n<p>A AND (B OR C) \u4e0e (A AND B) OR (A AND C) \u662f\u7b49\u4ef7\u8868\u8fbe\u793a\u3002<br \/>\n\u5047\u8bbe\u6709\u5982\u8868 3 \u6240\u793a\u7684\u4e00\u5f20\u8868\uff0c\u8981\u6267\u884c\u4e00\u4e2a AND \u64cd\u4f5c\u7b26\u5728\u524d\u7684\u8868\u8fbe\u793a<\/p>\n<p>SELECT * FROM Table1<br \/>\nWHERE (column1 = 1 AND column2 = &#8216;A&#8217;)<\/p>\n<p>OR (column1 = 1 AND column2 = &#8216;B&#8217;) |<br \/>\n\u8868 3. AND+OR \u67e5\u8be2<\/p>\n<p>Row#\t Colmun1\t Column2<br \/>\n1\t 3\t A<br \/>\n2\t 2\t B<br \/>\n3\t 1\t C<br \/>\n\u5f53\u6570\u636e\u5e93\u7cfb\u7edf\u6309\u7167\u67e5\u8be2\u8bed\u8fdb\u884c\u641c\u7d22\u65f6\uff0c\u5b83\u6309\u7167\u4e0b\u9762\u7684\u6b65\u9aa4\u6267\u884c\uff1a<\/p>\n<p>\u7d22\u5f15\u67e5\u627e column1 = 1, \u7ed3\u679c\u96c6 = \uff5brow 3\uff5d<br \/>\n\u7d22\u5f15\u67e5\u627e column2 = \u2018 A \u2019 , \u7ed3\u679c\u96c6 = \uff5brow1\uff5d<br \/>\nAND \u5408\u5e76\u7ed3\u679c\u96c6\uff0c\u7ed3\u679c\u96c6 = \uff5b\uff5d<br \/>\n\u7d22\u5f15\u67e5\u627e column 1 = 1, \u7ed3\u679c\u96c6 = \uff5brow 3\uff5d<br \/>\n\u7d22\u5f15\u67e5\u627e column 2 = \u2018 B \u2019 , \u7ed3\u679c\u96c6 = \uff5brow2\uff5d<br \/>\nAND \u5408\u5e76\u7ed3\u679c\u96c6\uff0c\u7ed3\u679c\u96c6 = \uff5b\uff5d<br \/>\nOR \u5408\u5e76\u7ed3\u96c6\uff0c\u7ed3\u679c\u96c6 = \uff5b\uff5d<br \/>\n\u73b0\u5728\u6839\u636e\u96c6\u5408\u7684\u5c55\u5f00\u6cd5\u5219\uff0c\u5bf9\u4e0a\u9762\u7684\u8bed\u53e5\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<p>SELECT * FROM Table1 WHERE column1 = 1 AND (column2 = &#8216;A&#8217; OR column2 = &#8216;B&#8217;)<br \/>\n\u6309\u7167\u65b0\u7684\u987a\u5e8f\u8fdb\u884c\u67e5\u641c\u7d22\u65f6\uff0c\u5b83\u6309\u7167\u4e0b\u9762\u7684\u6b65\u9aa4\u6267\u884c\uff1a<\/p>\n<p>\u7d22\u5f15\u67e5\u627e column2 = \u2018 A \u2019 , \u7ed3\u679c\u96c6 = \uff5brow1\uff5d<br \/>\n\u7d22\u5f15\u67e5\u627e column 2 = \u2018 B \u2019 , \u7ed3\u679c\u96c6 = \uff5brow2\uff5d<br \/>\nOR \u5408\u5e76\u7ed3\u96c6\uff0c\u7ed3\u679c\u96c6 = \uff5b\uff5d<br \/>\n\u7d22\u5f15\u67e5\u627e column1 = 1, \u7ed3\u679c\u96c6 = \uff5brow 3\uff5d<br \/>\nAND \u5408\u5e76\u7ed3\u679c\u96c6\uff0c\u7ed3\u679c\u96c6 = \uff5b\uff5d<br \/>\n\u7531\u6b64\u53ef\u89c1\u641c\u7d22\u6b21\u6570\u5c11\u4e86\u4e00\u6b21\u3002\u867d\u7136\u4e00\u4e9b\u6570\u636e\u5e93\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u7684\u8fdb\u884c\u8fd9\u6837\u7684\u8f6c\u6362\uff0c\u4f46\u662f\u5bf9\u4e8e\u7b80\u5355\u7684\u67e5\u8be2\u6765\u8bf4\uff0c\u8fd9\u6837\u7684\u8f6c\u6362\u8fd8\u662f\u6709\u597d\u5904\u7684\u3002<\/p>\n<p>\u975e (NOT)<\/p>\n<p>\u8ba9\u975e (NOT) \u8868\u8fbe\u793a\u8f6c\u6362\u6210\u66f4\u6613\u8bfb\u7684\u5f62\u5f0f\u3002\u7b80\u5355\u7684\u6761\u4ef6\u80fd\u901a\u8fc7\u5c06\u6bd4\u8f83\u64cd\u4f5c\u7b26\u8fdb\u884c\u53cd\u8f6c\u6765\u8fbe\u5230\u8f6c\u6362\u7684\u76ee\u7684\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8230; WHERE NOT (column1 > 5)<br \/>\n\u8f6c\u6362\u6210\uff1a<\/p>\n<p>&#8230; WHERE column1 <= 5\n\u6bd4\u8f83\u590d\u6742\u7684\u60c5\u51b5\uff0c\u6839\u636e\u96c6\u5408\u7684\u6469\u6839\u5b9a\u7406\uff1a\n\nNOT (A AND B) = (NOT A) OR (NOT B) \u548c NOT (A OR B) = (NOT A) AND (NOT B)\n\u6839\u636e\u8fd9\u4e00\u5b9a\u7406\uff0c\u53ef\u4ee5\u770b\u51fa\u5b83\u53ef\u4ee5\u81f3\u5c11\u4e8c\u6b21\u7684\u641c\u7d22\u6709\u53ef\u80fd\u51cf\u5c11\u4e3a\u4e00\u6b21\u3002\u5982\u4e0b\u7684\u67e5\u8be2\u6761\u4ef6\uff1a\n\n... WHERE NOT (column1 > 5 OR column2 = 7)<br \/>\n\u53ef\u4ee5\u8f6c\u6362\u6210\uff1a<\/p>\n<p>&#8230; WHERE column1 <= 5\nAND column2 <> 7 |<\/p>\n<p>\u4f46\u662f\uff0c\u5f53\u8f6c\u6362\u6210\u540e\u7684\u8868\u8fbe\u793a\u4e2d\u6709\u4e0d\u7b49\u64cd\u4f5c\u7b26 <>\uff0c\u90a3\u4e48\u6027\u80fd\u5c31\u4f1a\u4e0b\u964d\uff0c\u6bd5\u7adf\uff0c\u5728\u4e00\u4e2a\u503c\u5e73\u5747\u5206\u5e03\u7684\u96c6\u5408\u4e2d\uff0c\u4e0d\u7b49\u7684\u503c\u7684\u4e2a\u6570\u8981\u8fdc\u8fdc\u5927\u4e8e\u76f8\u7b49\u7684\u503c\u7684\u4e2a\u6570\uff0c\u6b63\u56e0\u4e3a\u5982\u6b64\uff0c\u4e00\u4e9b\u6570\u636e\u5e93\u7cfb\u7edf\u4e0d\u4f1a\u5bf9\u975e\u6bd4\u8f83\u8fdb\u884c\u7d22\u5f15\u641c\u7d22\uff0c\u4f46\u662f\u4ed6\u4eec\u4f1a\u4e3a\u5927\u4e8e\u6216\u5c0f\u4e8e\u8fdb\u884c\u7d22\u5f15\u641c\u7d22\uff0c\u6240\u4ee5\u53ef\u4ee5\u5c06\u4e0b\u9762\u7684\u67e5\u8be2\u8fdb\u884c\u5982\u4e0b\u8f6c\u6362\uff1a<\/p>\n<p>&#8230; WHERE NOT (column1 = 0)<br \/>\n\u8f6c\u6362\u6210\uff1a<\/p>\n<p>&#8230; WHERE column <0\nOR column > 0 |<\/p>\n<p>IN<\/p>\n<p>\u5f88\u591a\u4eba\u8ba4\u4e3a\u5982\u4e0b\u7684\u4e24\u4e2a\u67e5\u8be2\u6761\u4ef6\u6ca1\u6709\u4ec0\u4e48\u5dee\u522b\uff0c\u56e0\u4e3a\u5b83\u4eec\u8fd4\u56de\u7684\u7ed3\u679c\u96c6\u662f\u76f8\u540c\u7684\uff1a<\/p>\n<p>\u6761\u4ef6 1\uff1a<\/p>\n<p>&#8230; WHERE column1 = 5<br \/>\nOR column1 = 6 |<\/p>\n<p>\u6761\u4ef6 2\uff1a<\/p>\n<p>&#8230; WHERE column1 IN (5, 6)<br \/>\n\u8fd9\u6837\u7684\u60f3\u6cd5\u5e76\u4e0d\u5b8c\u5168\u6b63\u786e\uff0c\u5bf9\u4e8e\u5927\u591a\u6570\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u7cfb\u7edf\u6765\u8bf4\uff0cIN \u8981\u6bd4 OR \u6267\u884c\u7684\u5feb\u3002\u6240\u4ee5\u5982\u679c\u53ef\u4ee5\u7684\u8bdd\uff0c\u8981\u5c06 OR \u6362\u6210 IN<\/p>\n<p>\u5f53 IN \u64cd\u4f5c\u7b26\uff0c\u662f\u4e00\u7cfb\u5217\u5bc6\u96c6\u7684\u6574\u578b\u6570\u5b57\u65f6\uff0c\u6700\u597d\u662f\u67e5\u627e\u54ea\u4e9b\u503c\u4e0d\u7b26\u5408\u6761\u4ef6\uff0c\u800c\u4e0d\u662f\u67e5\u627e\u54ea\u4e9b\u503c\u7b26\u5408\u6761\u4ef6\uff0c\u56e0\u6b64\uff0c\u5982\u4e0b\u7684\u67e5\u8be2\u6761\u4ef6\u5c31\u5e94\u8be5\u8fdb\u884c\u5982\u4e0b\u7684\u8f6c\u6362\uff1a<\/p>\n<p>&#8230; WHERE column1 IN (1, 3, 4, 5)<br \/>\n\u8f6c\u6362\u6210\uff1a<\/p>\n<p>&#8230; WHERE column1 BETWEEN 1 AND 5<br \/>\nAND column1 <> 2<br \/>\n\u5f53\u4e00\u7cfb\u5217\u7684\u79bb\u6563\u7684\u503c\u8f6c\u6362\u6210\u7b97\u6570\u8868\u8fbe\u793a\u65f6\uff0c\u4e5f\u53ef\u83b7\u5f97\u540c\u6837\u7684\u6027\u80fd\u63d0\u9ad8\u3002<\/p>\n<p>UNION<\/p>\n<p>\u5728 SQL \u4e2d\uff0c\u4e24\u4e2a\u8868\u7684 UNION \u5c31\u662f\u4e24\u4e2a\u8868\u4e2d\u4e0d\u91cd\u590d\u7684\u503c\u7684\u96c6\u5408\uff0c\u5373 UNION \u64cd\u4f5c\u7b26\u8fd4\u8fd4\u56de\u7684\u4e24\u4e2a\u6216\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\u4e2d\u4e0d\u91cd\u590d\u884c\u7684\u96c6\u5408\u3002\u8fd9\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u5408\u5e76\u6570\u636e\u7684\u65b9\u6cd5\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u662f\u6700\u597d\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u67e5\u8be2 1\uff1a<\/p>\n<p>SELECT * FROM Table1<br \/>\nWHERE column1 = 5<\/p>\n<p>UNION<\/p>\n<p>SELECT * FROM Table1<\/p>\n<p>WHERE column2 = 5 |<\/p>\n<p>\u67e5\u8be2 2\uff1a<\/p>\n<p>SELECT DISTINCT * FROM Table1<br \/>\nWHERE column1 = 5<\/p>\n<p>OR column2 = 5 |<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0ccolumn1 \u548c column2 \u90fd\u6ca1\u6709\u7d22\u5f15\u3002\u5982\u679c\u67e5\u8be2 2 \u603b\u662f\u6bd4\u67e5\u8be2 1 \u6267\u884c\u7684\u5feb\u7684\u8bdd\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u5efa\u8bae\u603b\u662f\u5c06\u67e5\u8be2 1 \u8f6c\u6362\u6210\u67e5\u8be2 2\uff0c\u4f46\u662f\u6709\u4e00\u79cd\u60c5\u51b5\uff0c\u8fd9\u6837\u505a\u5728\u4e00\u4e9b\u6570\u636e\u5e93\u7cfb\u7edf\u4e2d\u53ef\u80fd\u4f1a\u5e26\u6765\u6027\u80fd\u53d8\u5dee\uff0c\u8fd9\u662f\u7531\u4e8e\u4e24\u4e2a\u4f18\u5316\u7f3a\u9677\u6240\u9020\u6210\u7684\u3002<\/p>\n<p>\u7b2c\u4e00\u4e2a\u4f18\u5316\u7f3a\u9677\u5c31\u662f\u5f88\u591a\u4f18\u5316\u5668\u53ea\u4f18\u5316\u4e00\u4e2a SELECT \u8bed\u53e5\u4e2d\u4e00\u4e2a WHERE \u8bed\u53e5\uff0c\u6240\u4ee5\u67e5\u8be2 1 \u7684\u4e24\u4e2a SELECT \u8bed\u53e5\u90fd\u88ab\u6267\u884c\u3002\u9996\u5148\u4f18\u5316\u5668\u6839\u636e\u67e5\u8be2\u6761\u4ef6 column1 = 5 \u4e3a\u771f\u6765\u67e5\u627e\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u6240\u6709\u884c\uff0c\u7136\u540e\u636e\u67e5\u8be2\u6761\u4ef6 column2 = 5 \u4e3a\u771f\u6765\u67e5\u627e\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u6240\u6709\u884c\uff0c\u5373\u4e24\u6b21\u8868\u626b\u63cf\uff0c\u56e0\u6b64\uff0c\u5982\u679c column1 = 5 \u6ca1\u6709\u7d22\u5f15\u7684\u8bdd\uff0c\u67e5\u8be2 1 \u5c06\u9700\u8981 2 \u500d\u4e8e\u67e5\u8be2 2 \u6240\u9700\u7684\u65f6\u95f4\u3002\u5982\u679c column1 = 5 \u6709\u7d22\u5f15\u7684\u8bdd\uff0c\u4ecd\u7136\u9700\u8981\u4e8c\u6b21\u626b\u63cf\uff0c\u4f46\u662f\u53ea\u6709\u5728\u67d0\u4e9b\u6570\u636e\u5e93\u7cfb\u7edf\u5b58\u5728\u4e00\u4e2a\u4e0d\u5e38\u89c1\u7684\u4f18\u5316\u7f3a\u9677\u5374\u5c06\u7b2c\u4e00\u4e2a\u4f18\u5316\u7f3a\u9677\u7ed9\u5f25\u8865\u4e86\u3002\u5f53\u4e00\u4e9b\u4f18\u5316\u5668\u53d1\u73b0\u67e5\u8be2\u4e2d\u5b58\u5728 OR \u64cd\u4f5c\u7b26\u65f6\uff0c\u5c31\u4e0d\u4f7f\u7528\u7d22\u5f15\u67e5\u8be2\uff0c\u6240\u4ee5\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5e76\u4e14\u53ea\u6709\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cUNION \u624d\u6bd4 OR \u6027\u80fd\u66f4\u9ad8\u3002\u8fd9\u79cd\u60c5\u51b5\u5f88\u5c11\u89c1\uff0c\u6240\u4ee5\u4ecd\u7136\u5efa\u8bae\u5927\u5bb6\u5f53\u5f85\u67e5\u8be2\u7684\u5217\u6ca1\u6709\u7d22\u5f15\u65f6\u4f7f\u7528 OR \u6765\u4ee3\u66ff UNION\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u4ee5\u4e0a\u662f\u4f5c\u8005\u5bf9\u5982\u4f55\u63d0\u9ad8 SQL \u6027\u80fd\u7684\u4e00\u4e9b\u603b\u7ed3\uff0c\u8fd9\u4e9b\u89c4\u5219\u5e76\u4e00\u5b9a\u5728\u6240\u6709\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u4e0a\u90fd\u80fd\u5e26\u6765\u6027\u80fd\u7684\u63d0\u9ad8\uff0c\u4f46\u662f\u5b83\u4eec\u4e00\u5b9a\u4e0d\u4f1a\u5bf9\u6570\u636e\u5e93\u7684\u6027\u80fd\u5e26\u6765\u4e0b\u964d\uff0c\u6240\u4ee5\u638c\u63e1\u5e76\u4f7f\u7528\u8fd9\u4e9b\u89c4\u5219\u53ef\u4ee5\u5bf9\u6570\u636e\u5e93\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u6709\u6240\u5e2e\u52a9\u3002\u672c\u6587\u603b\u7ed3\u7684\u662f\u4e00\u4e9b SQL \u6027\u80fd\u8c03\u4f18\u7684\u6bd4\u8f83\u521d\u7ea7\u7684\u65b9\u9762\uff0cSQL \u8c03\u4f18\u8fd8\u5305\u62ec Order by\uff0cGroup by \u4ee5\u53ca Index \u7b49\u7b49\uff0c\u4f5c\u8005\u5c06\u6765\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\u8fdb\u884c\u8bb2\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6839\u636e\u6211\u4eec\u7684\u7ecf\u9a8c\uff08\u7531\u5f88\u591a\u4e1a\u754c\u4e13\u5bb6\u8bc1\u660e\uff09\uff0c\u5728 SQL Server \u4e0a\u53d6\u5f97\u7684\u6027\u80fd\u63d0\u9ad8\u6709 80% \u6765\u81ea\u5bf9 SQL \u7f16\u7801\u7684\u6539\u8fdb\uff0c\u800c\u4e0d\u662f\u6765\u81ea\u4e8e\u5bf9\u4e8e\u914d\u7f6e\u6216\u7cfb\u7edf\u6027\u80fd\u7684\u8c03\u6574\u3002\u201d &#8212;\u51ef\u6587 \u514b\u83b1\u6069\u7b49\uff0cTransact-SQL Programming \u4f5c\u8005 \u201c\u7ecf\u9a8c\u8868\u660e 80%-90% \u7684\u6027\u80fd\u8c03\u4f18\u662f\u5728\u5e94\u7528\u7ea7\u505a\u7684\uff0c\u800c\u4e0d\u662f\u5728\u6570\u636e\u5e93\u7ea7\u201d<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19,17],"tags":[84,85],"_links":{"self":[{"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/posts\/136"}],"collection":[{"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=136"}],"version-history":[{"count":1,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/posts\/136\/revisions"}],"predecessor-version":[{"id":137,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/posts\/136\/revisions\/137"}],"wp:attachment":[{"href":"https:\/\/www.codeeel.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}