<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[沧海一粟]]></title> 
<link>http://www.dzhope.com/index.php</link> 
<description><![CDATA[Web系统架构与服务器运维,php开发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[沧海一粟]]></copyright>
<item>
<link>http://www.dzhope.com/post//</link>
<title><![CDATA[多项查询条件组合下的SQL语句生成]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[代码编程]]></category>
<pubDate>Tue, 03 Oct 2006 09:06:57 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	 作了一年的WEB应用，在这里面无疑核心就是数据的出出进进。而在作数据条件查询时，经常会遇到多项查询条件的组合，对于这种情况下SQL语句的生成经过这么长时间的实践加思考，最终给自己定下了一个规范性的编写方法。<br/><br/>举例如下：<br/><br/>现有数据库表，表名：student，表内字段如下：ID，GENDER，NAME，NUM，CLASSID。<br/><br/>有时会遇到的查询条件会是GENDER，NAME，NUM，CLASSID的任意组合，即每一项条件用户可以填也可以不填，如果按每一项条件字段NULL &nbsp;OR &nbsp;NOT &nbsp; NULL 来组合的话，会有16种情况。过去类似条件只是两项的情况下一般会用一种比较BC的办法就是根据这几种组合分别生成对应的SQL语句，但后来遇到一次比较郁闷的情况是查询条件到了6个，上种方法的可行性可想而知。<br/><br/>对于上例可以用如下构造方法(JAVA)。<br/><div class="code"><br/>String sql = &quot; SELECT * FORM student WHERE &quot; + <br/><br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot; ID = &quot; + (ID.equals(&quot;&quot;)?&quot;ID&quot;:ID) + <br/><br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot; AND GENDER = &quot; + (GENDER.equals(&quot;&quot;)?&quot;GENDER&quot;:GENDER) + <br/><br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot; AND NAME = &quot; + (NAME.equals(&quot;&quot;)?&quot;NAME&quot;:NAME) + <br/><br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot; AND NUM = &quot; + (NUM.equals(&quot;&quot;)?&quot;NUM&quot;:NUM) + <br/><br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot; AND CLASSID = &quot; + (CLASSID.equals(&quot;&quot;)?&quot;CLASSID&quot;:CLASSID);<br/></div><br/> &nbsp; &nbsp; &nbsp; &nbsp;在这里用到了?:三元运算符，在刚学JAVA的时候老师对这个运算只是简单一提没有想到这个运算符会在这里给你省这么多麻烦。<br/><br/>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] 多项查询条件组合下的SQL语句生成]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.dzhope.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>