{"id":451,"date":"2024-12-22T13:27:02","date_gmt":"2024-12-22T05:27:02","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=451"},"modified":"2024-12-22T13:28:31","modified_gmt":"2024-12-22T05:28:31","slug":"sql-%e8%af%ad%e5%8f%a5%e4%b8%ad-where-%e6%9d%a1%e4%bb%b6%e5%90%8e-%e5%86%99%e4%b8%8a-11-%e6%98%af%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d%ef%bc%9f%e8%af%a5%e6%94%b9%e6%80%8e%e4%b9%88%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/22\/sql-%e8%af%ad%e5%8f%a5%e4%b8%ad-where-%e6%9d%a1%e4%bb%b6%e5%90%8e-%e5%86%99%e4%b8%8a-11-%e6%98%af%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d%ef%bc%9f%e8%af%a5%e6%94%b9%e6%80%8e%e4%b9%88%e4%bc%98%e5%8c%96\/","title":{"rendered":"SQL \u8bed\u53e5\u4e2d where \u6761\u4ef6\u540e \u5199\u4e0a 1=1 \u662f\u4ec0\u4e48\u610f\u601d\uff1f\u5982\u4f55\u4f18\u5316\u6216\u907f\u514d\u4f7f\u7528"},"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 \u8bed\u53e5\u4e2d\uff0c<code>WHERE<\/code> \u6761\u4ef6\u540e\u5199\u4e0a <code>1=1<\/code> \u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6280\u5de7\uff0c\u901a\u5e38\u7528\u4e8e\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\u3002\u8ba9\u6211\u4eec\u8be6\u7ec6\u5206\u6790\u5b83\u7684\u542b\u4e49\u3001\u4f18\u7f3a\u70b9\u4ee5\u53ca\u662f\u5426\u9700\u8981\u4f18\u5316\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong><code>WHERE 1=1<\/code> \u7684\u542b\u4e49<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1=1<\/code> \u662f\u4e00\u4e2a\u6c38\u8fdc\u6210\u7acb\u7684\u6761\u4ef6\uff0c\u56e0\u4e3a <code>1<\/code> \u603b\u662f\u7b49\u4e8e <code>1<\/code>\uff0c\u5b83\u6c38\u8fdc\u8fd4\u56de <code>TRUE<\/code>\u3002\u56e0\u6b64\uff0c<code>WHERE 1=1<\/code> \u4f1a\u4f7f\u67e5\u8be2\u6761\u4ef6\u59cb\u7ec8\u6210\u7acb\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4e0d\u4f1a\u8fc7\u6ee4\u4efb\u4f55\u6570\u636e\u3002 <strong>\u793a\u4f8b\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT * FROM employees WHERE 1=1;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6761\u67e5\u8be2\u4f1a\u8fd4\u56de <code>employees<\/code> \u8868\u4e2d\u7684\u6240\u6709\u8bb0\u5f55\uff0c\u56e0\u4e3a <code>WHERE 1=1<\/code> \u4e0d\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u4efb\u4f55\u7b5b\u9009\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u4e3a\u4ec0\u4e48\u4f1a\u4f7f\u7528 <code>WHERE 1=1<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>WHERE 1=1<\/code> \u672c\u8eab\u5e76\u4e0d\u4f1a\u5bf9\u67e5\u8be2\u4ea7\u751f\u5b9e\u9645\u7684\u5f71\u54cd\uff0c\u4f46\u5b83\u7ecf\u5e38\u88ab\u7528\u4f5c\u4e00\u79cd\u7f16\u7801\u6280\u5de7\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\u5e38\u89c1\u60c5\u51b5\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.1. <strong>\u52a8\u6001\u67e5\u8be2\u6784\u5efa<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6784\u5efa\u52a8\u6001 SQL \u67e5\u8be2\u65f6\uff0c\u5c24\u5176\u662f\u5728\u6709\u591a\u4e2a\u6761\u4ef6\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528 <code>WHERE 1=1<\/code> \u53ef\u4ee5\u7b80\u5316\u67e5\u8be2\u6784\u5efa\u7684\u903b\u8f91\uff0c\u907f\u514d\u6761\u4ef6\u62fc\u63a5\u65f6\u51fa\u9519\u3002\u4f8b\u5982\uff0c\u5728\u52a8\u6001\u6784\u5efa SQL \u8bed\u53e5\u65f6\uff0c\u53ef\u80fd\u6709\u591a\u4e2a\u6761\u4ef6\uff0c\u52a0\u5165 <code>WHERE 1=1<\/code> \u540e\uff0c\u53ef\u4ee5\u7b80\u5355\u5730\u7edf\u4e00\u5730\u5728\u6bcf\u4e2a\u6761\u4ef6\u524d\u52a0\u4e0a <code>AND<\/code>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e0d\u4f7f\u7528 <code>1=1<\/code> \u7684\u60c5\u51b5\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT * FROM employees WHERE age = 30 AND name = 'Alice' AND department = 'HR';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u6761\u4ef6\u53d8\u5316\u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u9047\u5230\u4ee5\u4e0b\u95ee\u9898\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u7b2c\u4e00\u4e2a\u6761\u4ef6\u6ca1\u6709\u4f7f\u7528 <code>AND<\/code>\uff0c\u5c31\u9700\u8981\u989d\u5916\u5904\u7406\u7b2c\u4e00\u4e2a\u6761\u4ef6\u7684\u62fc\u63a5\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528 <code>1=1<\/code> \u7684\u60c5\u51b5\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT * FROM employees WHERE 1=1 AND age = 30 AND name = 'Alice' AND department = 'HR';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 <code>WHERE 1=1<\/code> \u540e\uff0c\u6240\u6709\u6761\u4ef6\u90fd\u53ef\u4ee5\u7edf\u4e00\u52a0\u4e0a <code>AND<\/code>\uff0c\u5373\u4f7f\u67d0\u4e9b\u6761\u4ef6\u7f3a\u5931\uff0c\u4ee3\u7801\u4e5f\u80fd\u66f4\u52a0\u7edf\u4e00\uff0c\u907f\u514d\u62fc\u63a5\u65f6\u51fa\u9519\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.2. <strong>\u8c03\u8bd5\u548c\u6761\u4ef6\u6ce8\u91ca<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8c03\u8bd5 SQL \u67e5\u8be2\u6216\u9010\u6b65\u6d4b\u8bd5\u65f6\uff0c\u5f00\u53d1\u8005\u6709\u65f6\u4f1a\u6682\u65f6\u4f7f\u7528 <code>WHERE 1=1<\/code> \u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u65b9\u4fbf\u540e\u7eed\u9010\u6b65\u6dfb\u52a0\u6761\u4ef6\uff0c\u800c\u4e0d\u5f71\u54cd\u67e5\u8be2\u8bed\u53e5\u7684\u7ed3\u6784\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong><code>WHERE 1=1<\/code> \u7684\u4f18\u7f3a\u70b9<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">3.1. <strong>\u4f18\u70b9<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7b80\u5316\u52a8\u6001\u67e5\u8be2\u7684\u6784\u5efa<\/strong>\uff1a\u5982\u524d\u6240\u8ff0\uff0c<code>WHERE 1=1<\/code> \u5728\u52a8\u6001\u67e5\u8be2\u6784\u5efa\u65f6\u975e\u5e38\u65b9\u4fbf\uff0c\u53ef\u4ee5\u907f\u514d\u6761\u4ef6\u62fc\u63a5\u65f6\u7684\u590d\u6742\u6027\u3002<\/li>\n\n\n\n<li><strong>\u4ee3\u7801\u7684\u901a\u7528\u6027<\/strong>\uff1a\u5728\u4e00\u4e9b\u7cfb\u7edf\u4e2d\uff0c\u4f7f\u7528 <code>1=1<\/code> \u53ef\u4ee5\u8ba9\u6240\u6709\u67e5\u8be2\u5728\u683c\u5f0f\u4e0a\u7edf\u4e00\uff0c\u65b9\u4fbf\u540e\u671f\u7ef4\u62a4\u548c\u4fee\u6539\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3.2. <strong>\u7f3a\u70b9<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5f71\u54cd\u53ef\u8bfb\u6027<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u67e5\u8be2\u8bed\u53e5\uff0c<code>WHERE 1=1<\/code> \u770b\u8d77\u6765\u662f\u591a\u4f59\u7684\uff0c\u53ef\u80fd\u4f1a\u8ba9\u4eba\u89c9\u5f97\u4e0d\u5fc5\u8981\uff0c\u589e\u52a0\u4e86\u4ee3\u7801\u7684\u590d\u6742\u6027\u3002<\/li>\n\n\n\n<li><strong>\u6f5c\u5728\u6027\u80fd\u5f71\u54cd<\/strong>\uff1a\u867d\u7136 <code>WHERE 1=1<\/code> \u4e0d\u4f1a\u5bf9\u67e5\u8be2\u7684\u7ed3\u679c\u4ea7\u751f\u4efb\u4f55\u5b9e\u9645\u5f71\u54cd\uff0c\u4f46\u5728\u4e00\u4e9b\u6570\u636e\u5e93\u5f15\u64ce\u4e2d\uff0c\u8fc7\u591a\u7684\u65e0\u7528\u6761\u4ef6\u53ef\u80fd\u4f1a\u5f71\u54cd\u4f18\u5316\u5668\u7684\u6267\u884c\u8ba1\u5212\uff0c\u5bfc\u81f4\u6027\u80fd\u7684\u5fae\u5c0f\u4e0b\u964d\uff08\u4e0d\u8fc7\uff0c\u73b0\u4ee3\u6570\u636e\u5e93\u4f18\u5316\u5668\u901a\u5e38\u4f1a\u4f18\u5316\u6389\u8fd9\u4e9b\u65e0\u7528\u6761\u4ef6\uff0c\u6240\u4ee5\u8fd9\u79cd\u5f71\u54cd\u901a\u5e38\u662f\u975e\u5e38\u5fae\u5c0f\u7684\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u5982\u4f55\u4f18\u5316\u6216\u907f\u514d\u4f7f\u7528 <code>WHERE 1=1<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u89c9\u5f97 <code>WHERE 1=1<\/code> \u4e0d\u5fc5\u8981\uff0c\u6216\u8005\u4e0d\u559c\u6b22\u8fd9\u79cd\u5199\u6cd5\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u51e0\u79cd\u4f18\u5316\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.1. <strong>\u907f\u514d\u5728\u9759\u6001\u67e5\u8be2\u4e2d\u4f7f\u7528 <code>WHERE 1=1<\/code><\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u7b80\u5355\u7684\u67e5\u8be2\u8bed\u53e5\uff0c\u6ca1\u6709\u5fc5\u8981\u4f7f\u7528 <code>WHERE 1=1<\/code>\u3002\u76f4\u63a5\u7f16\u5199\u6e05\u6670\u3001\u7b80\u6d01\u7684\u67e5\u8be2\u8bed\u53e5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM employees WHERE age = 30 AND name = 'Alice' AND department = 'HR';<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.2. <strong>\u5728\u52a8\u6001\u67e5\u8be2\u4e2d\u667a\u80fd\u62fc\u63a5\u6761\u4ef6<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u662f\u5728\u52a8\u6001\u67e5\u8be2\u4e2d\u4f7f\u7528 <code>WHERE 1=1<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u7a0b\u65b9\u5f0f\u52a8\u6001\u5224\u65ad\u662f\u5426\u9700\u8981\u62fc\u63a5\u6761\u4ef6\uff0c\u800c\u4e0d\u662f\u4e00\u5f00\u59cb\u5c31\u62fc\u63a5 <code>1=1<\/code>\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5148\u5224\u65ad\u662f\u5426\u5b58\u5728\u4efb\u4f55\u6761\u4ef6\uff0c\u5982\u679c\u6709\u6761\u4ef6\u518d\u52a0 <code>WHERE<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>query = \"SELECT * FROM employees\"\nconditions = &#91;]\n\nif age_condition:\n    conditions.append(f\"age = {age_condition}\")\nif name_condition:\n    conditions.append(f\"name = '{name_condition}'\")\n\nif conditions:\n    query += \" WHERE \" + \" AND \".join(conditions)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.3. <strong>\u4f7f\u7528 <code>CASE<\/code> \u6216\u5176\u4ed6\u903b\u8f91\u6765\u52a8\u6001\u5904\u7406<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u9700\u8981\u5728\u67e5\u8be2\u4e2d\u52a8\u6001\u63a7\u5236\u67d0\u4e9b\u6761\u4ef6\uff0c<code>CASE<\/code> \u8868\u8fbe\u5f0f\u6216\u8005\u903b\u8f91\u63a7\u5236\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6839\u636e\u4e0d\u540c\u7684\u60c5\u51b5\u6267\u884c\u4e0d\u540c\u7684\u67e5\u8be2\u903b\u8f91\uff0c\u800c\u4e0d\u4f9d\u8d56 <code>WHERE 1=1<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>WHERE 1=1<\/code> \u672c\u8eab\u662f\u4e00\u4e2a\u6c38\u8fdc\u4e3a\u771f\u7684\u6761\u4ef6\uff0c\u901a\u5e38\u7528\u4e8e\u52a8\u6001 SQL \u67e5\u8be2\u6784\u5efa\u4e2d\uff0c\u76ee\u7684\u662f\u7b80\u5316\u591a\u4e2a\u6761\u4ef6\u62fc\u63a5\u7684\u903b\u8f91\u3002<\/li>\n\n\n\n<li>\u5728\u7b80\u5355\u7684\u67e5\u8be2\u4e2d\uff0c\u5b83\u901a\u5e38\u662f\u591a\u4f59\u7684\uff0c\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u3002<\/li>\n\n\n\n<li>\u5bf9\u4e8e\u52a8\u6001\u67e5\u8be2\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u907f\u514d\u590d\u6742\u7684\u6761\u4ef6\u62fc\u63a5\u95ee\u9898\uff0c\u4f46\u5982\u679c\u53ef\u80fd\uff0c\u8003\u8651\u4f7f\u7528\u66f4\u6e05\u6670\u7684\u65b9\u5f0f\u6765\u5904\u7406\u67e5\u8be2\u6761\u4ef6\uff0c\u5c24\u5176\u662f\u5728\u7f16\u5199\u9759\u6001\u67e5\u8be2\u65f6\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u6b63\u5728\u5904\u7406\u52a8\u6001 SQL \u6216\u8005\u9700\u8981\u9891\u7e41\u62fc\u63a5\u67e5\u8be2\u6761\u4ef6\uff0c<code>WHERE 1=1<\/code> \u786e\u5b9e\u662f\u4e00\u4e2a\u6709\u7528\u7684\u6280\u5de7\uff0c\u4f46\u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\u5e94\u907f\u514d\u4f7f\u7528\uff0c\u4fdd\u6301\u4ee3\u7801\u7b80\u6d01\u660e\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 SQL \u8bed\u53e5\u4e2d\uff0cWHERE \u6761\u4ef6\u540e\u5199\u4e0a 1=1 \u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6280\u5de7\uff0c\u901a\u5e38\u7528\u4e8e\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\u3002\u8ba9\u6211\u4eec\u8be6\u7ec6\u5206\u6790\u5b83\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-451","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/451","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=451"}],"version-history":[{"count":3,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/451\/revisions"}],"predecessor-version":[{"id":454,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/451\/revisions\/454"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}