﻿{"id":595,"date":"2010-01-10T00:35:00","date_gmt":"2010-01-09T16:35:00","guid":{"rendered":"http:\/\/blog.jixin.ntxz.net\/?p=595"},"modified":"2010-07-30T15:40:40","modified_gmt":"2010-07-30T07:40:40","slug":"%e6%9f%a5%e7%9c%8b%e6%95%b0%e6%8d%ae%e5%ba%93%e6%97%a5%e5%bf%97%ef%bc%8c%e5%8f%91%e7%8e%b0where%e5%89%8d%e9%83%bd%e6%9c%89%e4%b8%80%e4%b8%aa%e2%80%9c11%e2%80%9d","status":"publish","type":"post","link":"http:\/\/www.ntxz.net\/?p=595","title":{"rendered":"\u67e5\u770b\u6570\u636e\u5e93\u65e5\u5fd7\uff0c\u53d1\u73b0Where\u524d\u90fd\u6709\u4e00\u4e2a\u201c1=1\u201d"},"content":{"rendered":"<p>\u67d0DBA\u5728\u67e5\u770b\u81ea\u5df1\u7684<span class=t_tag onclick=tagshow(event) href=\"http:\/\/blog.cnbytes.net\/tag.php?name=%CA%FD%BE%DD\">\u6570\u636e<\/span>\u5e93\u65e5\u5fd7\u7684\u65f6\u5019\uff0c\u770b\u5230\u4e86\u6570\u636e\u5e93<span class=t_tag onclick=tagshow(event) href=\"http:\/\/blog.cnbytes.net\/tag.php?name=%B7%FE%CE%F1%C6%F7\">\u670d\u52a1\u5668<\/span>\u4e0a\u51fa\u73b0\u4e86\u5f88\u591a\u5f88\u602a\u5f02\u7684SQL\u7684Where\u6761\u4ef6\u8bed\u53e5\uff0c\u662f\u4e0b\u9762\u8fd9\u4e2a\u6837\u5b50\uff1a\uff08\u6240\u6709\u7684where\u8bed\u53e5\u524d\u90fd\u6709\u4e86\u4e00\u4e2a\u53eb\u201c1=1\u201d\u7684\u5b50\u6761\u4ef6\uff09\u5475\u5475\u3002<font style=\"FONT-SIZE: 0px; COLOR: #fff\">3 U0 {* n: u) I- y# E1 G* z6 ~2 V<\/font><\/p>\n<p align=center><span id=attach_11110 onmouseover=\"showMenu(this.id, 0, 1)\" style=\"DISPLAY: none; POSITION: absolute\"><img decoding=\"async\" src=\"http:\/\/tech.techweb.com.cn\/images\/default\/attachimg.gif\" border=0><\/span> <img decoding=\"async\" class=\"http:\/\/blog.cnbytes.net\/\" id=aimg_11110 onclick=\"zoom(this, this.src)\" alt=sql.where.jpg src=\"http:\/\/tech.techweb.com.cn\/attachment.php?aid=11110&#038;noupdate=yes\" status=\"http:\/\/blog.cnbytes.net\/2\" initialized=\"true\" outfunc=\"null\" X=\"355\" Y=\"499\" w=\"460\" h=\"631\"> <\/p>\n<div class=t_attach id=aimg_11110_menu style=\"DISPLAY: none; Z-INDEX: 999; LEFT: 355px; CLIP: rect(auto auto auto auto); POSITION: absolute; TOP: 457px\" initialized=\"true\" w=\"142\" h=\"42\" ctrlkey=\"aimg_11110\"><a title=sql.where.jpg href=\"http:\/\/tech.techweb.com.cn\/attachment.php?aid=11110&amp;k=fa05c51078f5b1dc8ec282119fb453f7&amp;t=1263025552&amp;nothumb=yes&amp;sid=f9e6UDQRmjyEjmPDdBchSTGSaE%2BCtyOsjkX8Flc34pIKpSQ\" target=_blank><strong><span class=t_tag onclick=tagshow(event) href=\"http:\/\/blog.cnbytes.net\/tag.php?name=%CF%C2%D4%D8\">\u4e0b\u8f7d<\/span><\/strong><\/a> (69.6 KB)<\/p>\n<div class=t_smallfont>2009-12-7 15:06<\/div>\n<\/div>\n<p><span style=\"DISPLAY: none\">! r&nbsp;&nbsp;i( W8 z# t+ @) |2 a<\/span> <\/p>\n<p><font style=\"FONT-SIZE: 0px; COLOR: #fff\">* I- |+ g&#8221; N1 N\/ S&#8221; X% w<\/font><br \/>\u8981\u7406\u89e3\u8fd9\u4e2a\u4e8b\u60c5\u7684\u539f\u56e0\u5176\u5b9e\u5e76\u4e0d\u96be\u3002\u53ea\u8981\u4f60\u662f\u4e00\u4e2a\u7f16\u5199\u6570\u636e\u5e93\u7684\u7a0b\u5e8f\u5458\uff0c\u4f60\u5c31\u4f1a\u77e5\u9053\u2014\u2014\u52a8\u6001\u751f\u6210where\u540e\u7684\u6761\u4ef6\u7684\u201c\u9ebb\u70e6\u201d\uff0c\u90a3\u5c31\u662f\u6761\u4ef6\u7684\u201c\u5206\u9694\u201d-and\u6216or\u3002\u4e0b\u9762\u542c\u6211\u6162\u6162\u8bf4\u6765\u3002<br \/><span style=\"DISPLAY: none\">, i7 A&#8217; e* p&#8221; q$ c\/ T( u<\/span><font style=\"FONT-SIZE: 0px; COLOR: #fff\">6 X; A9 p9 d( c. `6 m- U<\/font><\/p>\n<p><span style=\"DISPLAY: none\">&amp; k# u0 I$ c8 t$ k- ]<\/span>\u6211\u4eec\u77e5\u9053\uff0c\u5927\u591a\u6570\u7684\u67e5\u8be2\u8868\u5355\u90fd\u9700\u8981\u7528\u6237\u8f93\u51fa\u4e00\u5806\u67e5\u8be2\u6761\u4ef6\uff0c\u7136\u540e\u6211\u4eec\u7684\u7a0b\u5e8f\u5728\u540e\u53f0\u8981\u628a\u8fd9\u4e9b\u5b50\u6761\u4ef6\u7528and\u7ec4\u5408\u8d77\u6765\u3002\u4e8e\u662f\uff0c\u628aand\u52a0\u5728\u5404\u4e2a\u6761\u4ef6\u7684\u4e2d\u95f4\u5c31\u6210\u4e3a\u4e86\u4e00\u4ef6\u201c\u96be\u4e8b\u201d\uff0c\u56e0\u4e3a\u4f60\u7684\u7a0b\u5e8f\u9700\u8981\u5224\u65ad\uff1a<font style=\"FONT-SIZE: 0px; COLOR: #fff\">0 a&amp; F9 w, D9 A. P<\/font><\/p>\n<ul>\n<li>\u5982\u679c\u6ca1\u6709\u6761\u4ef6\u7684\u8bdd\uff0c\u5219\u4e0d\u9700\u8981where\n<li>\u5982\u679c\u53ea\u6709\u4e00\u4e2a\u6761\u4ef6\u7684\u8bdd\uff0c\u4e0d\u9700\u8981and.\n<li>\u5982\u679c\u6709\u591a\u4e2a\u6761\u4ef6\u7684\u8bdd\uff0c\n<ul>\n<li>\u5982\u679cand\u662f\u6301\u7eed\u52a0\u5728\u6bcf\u4e2a\u6761\u4ef6\u540e\u9762\u7684\u8bdd\uff0c\u90a3\u4e48\u5c31\u8981\u5224\u65ad\u662f\u5426\u662f\u6700\u540e\u4e00\u4e2a\u6761\u4ef6\uff0c\u56e0\u4e3a\u6700\u540e\u4e00\u4e2a\u4e0d\u80fd\u52a0and.\n<li>\u540c\u6837\uff0c\u5982\u679cand\u662f\u8981\u52a0\u5728\u6bcf\u4e2a\u6761\u4ef6\u524d\u9762\u7684\u8bdd\uff0c\u4f60\u5c31\u9700\u8981\u5224\u65ad\u662f\u5426\u662f\u7b2c\u4e00\u4e2a\uff0c\u5982\u679c\u662f\uff0c\u90a3\u5c31\u4e0d\u52a0\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u771f\u662f***\u592a\u70e6\u4e86\uff0c\u6240\u4ee5\uff0c\u7f16\u7a0b\u201c\u5927\u62ff\u201d\u5f15\u5165\u4e86\u201c1=1\u201d\u6761\u4ef6\u8bed\u53e5\u3002\u4e8e\u662f\uff0c\u7f16\u7a0b\u7684\u96be\u5ea6\u5927\u5e45\u5ea6\u4e0b\u964d\uff0c\u4f60\u53ef\u4ee5\u7528\u5355\u4e00\u7684\u65b9\u5f0f\u6765\u5904\u7406\u8fd9\u82e5\u5e72\u7684\u67e5\u8be2\u5b50\u6761\u4ef6\u4e86\u3002\u800c1=1\u5e94\u8be5\u4f1a\u88ab\u6570\u636e\u5e93\u5f15\u64ce\u4f18\u5316\u65f6\u7ed9\u53bb\u6389\u4e86\u3002 <\/p>\n<div class=blockcode>\n<div id=code0>\n<ol>\n<li>&lt;pre&gt;&lt;code&gt;$query = &#8220;SELECT name FROM table WHERE 1=1 &#8220;;&nbsp;&nbsp;\n<li>\n<li>\n<li>\n<li>foreach($clauses as $key =&gt; $value){&nbsp;&nbsp;\n<li>\n<li>$query .= &#8221; AND &#8220;.escape($key).&#8221;=&#8221;.escape($value).&#8221; &#8220;;&nbsp;&nbsp;\n<li>\n<li>}&nbsp;&nbsp;\n<li>\n<li>&lt;\/code&gt;&lt;\/pre&gt; <\/li>\n<\/ol>\n<\/div>\n<p><em onclick=\"copycode($('code0'));\">\u590d\u5236\u4ee3\u7801<\/em><\/div>\n<p>\u5475\u5475\uff0c<strong>DBA\u600e\u4e48\u80fd\u591f\u7406\u89e3\u6211\u4eec\u75af\u72c2\u7a0b\u5e8f\u5458\u7684\u7528\u5fc3\u826f\u82e6\u554a<\/strong>\u3002<br \/><span style=\"DISPLAY: none\">0 l6 n1 \\) R! L+ b&amp; {&#8216; W<\/span>\u53e6\u5916\uff0c\u5728\u8fd9\u91cc\u8bf4\u4e00\u4e0b\uff0c\u8fd9\u6837\u7684\u505a\u6cd5\u770b\u4f3c\u5f88\u611a\u8822\uff0c\u4f46\u5f88\u6709\u6548\uff0c\u5728<span class=t_tag onclick=tagshow(event) href=\"http:\/\/blog.cnbytes.net\/tag.php?name=PHP\">PHP<\/span>\u7684\u4e16\u754c\u4e2d\uff0c\u4e5f\u6709\u4eba\u4f7f\u7528\u4e0b\u9762\u8fd9\u6837\u7684\u505a\u6cd5\u2014\u2014\u4f7f\u7528\u4e86PHP\u7684implode\u51fd\u6570\u3002 <\/p>\n<div class=blockcode>\n<div id=code1>\n<ol>\n<li>&lt;pre&gt;&lt;code&gt;\/**\n<li>* @param string $base base of query, e.g. UPDATE table SET\n<li>* @param string $logic logic for concatenating $assoc, e.g. AND\n<li>* @param array $assoc associative array of `field`=&gt;&#8217;value&#8217; pairs to concatenate and append to $base\n<li>* @param string $suffix additional clauses, e.g. LIMIT 0,30\n<li>* @return string\n<li>*\/\n<li>function construct_sql($base, $logic, $clauses, $suffix=&#8221;)\n<li>{\n<li>&nbsp; &nbsp; \/\/ initialise array to avoid warnings\/notices on some PHP installations\n<li>&nbsp; &nbsp; $queries = array();\n<li>\n<li>&nbsp; &nbsp; \/\/ create array of strings to be glued together by logic\n<li>&nbsp; &nbsp; foreach($clauses as $key =&gt; $value)\n<li>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;$queries[] = &#8220;`&#8221; . escape($key) . &#8220;`='&#8221; . escape($value) . &#8220;&#8216;&#8221;;\n<li>\n<li>&nbsp; &nbsp; \/\/ add a space in case $base doesn&#8217;t have a space at the end and glue clauses together\n<li>&nbsp; &nbsp; $query .= &#8221; &#8221; . implode(&#8221; $logic &#8220;,$queries) . &#8221; &#8221; . $suffix . &#8220;;&#8221;;\n<li>\n<li>&nbsp; &nbsp; return $query;\n<li>}\n<li>\n<li>\/**\n<li>* @param string $str string to escape for intended use\n<li>* @return string\n<li>*\/\n<li>function escape($str)\n<li>{\n<li>&nbsp; &nbsp; return mysql_real_escape_string($str);\n<li>}\n<li>&lt;\/code&gt;&lt;\/pre&gt;<\/li>\n<\/ol>\n<\/div>\n<p><em onclick=\"copycode($('code1'));\">\u590d\u5236\u4ee3\u7801<\/em><\/div>\n<p>\u4e8e\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a\uff08\u4e3a\u4ec0\u4e48\u6211\u4eec\u8981\u5728update\u8bed\u53e5\u540e\u52a0\u4e0a\u201cLimit 1\u201d\u5462\uff1f\u8fd9\u4e2a\u5173\u7cfb\u5230\u6027\u80fd\u95ee\u9898\uff0c\u5173\u4e8e\u8fd9\u65b9\u9762\u7684\u8bdd\u9898\uff0c\u4f60\u53ef\u4ee5\u67e5\u770b\u672c\u7ad9\u7684\u300a<a href=\"http:\/\/coolshell.cn\/?p=1846\" target=_blank>MySQL\u6027\u80fd\u4f18\u5316\u7684\u6700\u4f7320+\u6761<span class=t_tag onclick=tagshow(event) href=\"http:\/\/blog.cnbytes.net\/tag.php?name=%BE%AD%D1%E9\">\u7ecf\u9a8c<\/span><\/a>\u300b\uff09 <\/p>\n<div class=blockcode>\n<div id=code2>\n<ol>\n<li>&lt;pre&gt;&lt;code&gt;$updates = array(\n<li>&nbsp; &nbsp; &#8216;field1&#8217;=&gt;&#8217;val1&#8217;\n<li>&nbsp; &nbsp; &#8216;field2&#8217;=&gt;&#8217;val2&#8217;\n<li>);\n<li>$wheres = array(\n<li>&nbsp; &nbsp; &#8216;field1&#8217;=&gt;&#8217;cond1&#8217;,\n<li>&nbsp; &nbsp; &#8216;field2&#8217;=&gt;&#8217;cond2&#8217;\n<li>);\n<li>echo construct_sql(construct_sql(&#8220;UPDATE table SET&#8221;, &#8220;, &#8220;, $updates) . &#8221; WHERE &#8220;, &#8221; AND &#8220;, $wheres),&#8221;LIMIT 1&#8221;);\n<li>&lt;\/code&gt;&lt;\/pre&gt;\n<li>&lt;pre&gt;&lt;\/pre&gt;<\/li>\n<\/ol>\n<\/div>\n<p><em onclick=\"copycode($('code2'));\">\u590d\u5236\u4ee3\u7801<\/em><\/div>\n<p>\u4e0b\u9762\u662f\u8f93\u51fa\u7ed3\u679c\uff1a <\/p>\n<div class=blockcode>\n<div id=code3>\n<ol>\n<li>UPDATE table SET `field1`=&#8217;val1&#8242;, `field2`=&#8217;val2&#8242; WHERE `field1`=&#8217;cond1&#8242; AND `field2`=&#8217;cond2&#8242; LIMIT 1;<\/li>\n<\/ol>\n<\/div>\n<p><em onclick=\"copycode($('code3'));\">\u590d\u5236\u4ee3\u7801<\/em><\/div>\n<p>\uff08\u5168\u6587\u5b8c\uff09<font style=\"FONT-SIZE: 0px; COLOR: #fff\">3 c! T9 ]&amp; A&#8217; s4 w<\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u67d0DBA\u5728\u67e5\u770b\u81ea\u5df1\u7684\u6570\u636e\u5e93\u65e5\u5fd7\u7684\u65f6\u5019\uff0c\u770b\u5230\u4e86\u6570\u636e\u5e93\u670d\u52a1\u5668\u4e0a\u51fa\u73b0\u4e86\u5f88\u591a\u5f88\u602a\u5f02\u7684SQL\u7684Where\u6761\u4ef6\u8bed\u53e5\uff0c\u662f\u4e0b\u9762 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,8],"tags":[],"class_list":["post-595","post","type-post","status-publish","format-standard","hentry","category-1","category-oldblog"],"views":332,"_links":{"self":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=595"}],"version-history":[{"count":1,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/595\/revisions"}],"predecessor-version":[{"id":796,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/595\/revisions\/796"}],"wp:attachment":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=595"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}