<?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/147/</link>
<title><![CDATA[php实现模糊查询功能]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[代码编程]]></category>
<pubDate>Wed, 18 Oct 2006 02:38:36 +0000</pubDate> 
<guid>http://www.dzhope.com/post/147/</guid> 
<description>
<![CDATA[ 
	<br/>$sql=&quot;select * from table_name where field_name like &#039;%$var%&#039;&quot;;<br/>$result=mysql_query($sql) or die;<br/><br/>MySQL提供标准的SQL模式匹配，以及一种基于象Unix实用程序如vi、grep和sed的扩展正则表达式<br/><br/>模式匹配的格式。 <br/><br/>SQL的模式匹配允许你使用“_”匹配任何单个字符，而“%”匹配任意数目字符(包括零个字符)。<br/><br/>在 MySQL中，SQL的模式缺省是忽略大小写的。下面显示一些例子。注意在你使用SQL模式时，你不<br/><br/>能使用=或!=；而使用LIKE或NOT LIKE比较操作符。 <br/><br/>为了找出以“b”开头的名字： <br/><br/>mysql&gt; SELECT * FROM pet WHERE name LIKE &quot;b%&quot;;<br/>+--------+--------+---------+------+------------+------------+<br/>&#124; name &nbsp; &#124; owner &nbsp;&#124; species &#124; sex &nbsp;&#124; birth &nbsp; &nbsp; &nbsp;&#124; death &nbsp; &nbsp; &nbsp;&#124;<br/>+--------+--------+---------+------+------------+------------+<br/>&#124; Buffy &nbsp;&#124; Harold &#124; dog &nbsp; &nbsp; &#124; f &nbsp; &nbsp;&#124; 1989-05-13 &#124; NULL &nbsp; &nbsp; &nbsp; &#124;<br/>&#124; Bowser &#124; Diane &nbsp;&#124; dog &nbsp; &nbsp; &#124; m &nbsp; &nbsp;&#124; 1989-08-31 &#124; 1995-07-29 &#124;<br/>+--------+--------+---------+------+------------+------------+<br/><br/>为了找出以“fy”结尾的名字： <br/><br/>mysql&gt; SELECT * FROM pet WHERE name LIKE &quot;%fy&quot;;<br/>+--------+--------+---------+------+------------+-------+<br/>&#124; name &nbsp; &#124; owner &nbsp;&#124; species &#124; sex &nbsp;&#124; birth &nbsp; &nbsp; &nbsp;&#124; death &#124;<br/>+--------+--------+---------+------+------------+-------+<br/>&#124; Fluffy &#124; Harold &#124; cat &nbsp; &nbsp; &#124; f &nbsp; &nbsp;&#124; 1993-02-04 &#124; NULL &nbsp;&#124;<br/>&#124; Buffy &nbsp;&#124; Harold &#124; dog &nbsp; &nbsp; &#124; f &nbsp; &nbsp;&#124; 1989-05-13 &#124; NULL &nbsp;&#124;<br/>+--------+--------+---------+------+------------+-------+<br/><br/>为了找出包含一个“w”的名字： <br/><br/>mysql&gt; SELECT * FROM pet WHERE name LIKE &quot;%w%&quot;;<br/>+----------+-------+---------+------+------------+------------+<br/>&#124; name &nbsp; &nbsp; &#124; owner &#124; species &#124; sex &nbsp;&#124; birth &nbsp; &nbsp; &nbsp;&#124; death &nbsp; &nbsp; &nbsp;&#124;<br/>+----------+-------+---------+------+------------+------------+<br/>&#124; Claws &nbsp; &nbsp;&#124; Gwen &nbsp;&#124; cat &nbsp; &nbsp; &#124; m &nbsp; &nbsp;&#124; 1994-03-17 &#124; NULL &nbsp; &nbsp; &nbsp; &#124;<br/>&#124; Bowser &nbsp; &#124; Diane &#124; dog &nbsp; &nbsp; &#124; m &nbsp; &nbsp;&#124; 1989-08-31 &#124; 1995-07-29 &#124;<br/>&#124; Whistler &#124; Gwen &nbsp;&#124; bird &nbsp; &nbsp;&#124; NULL &#124; 1997-12-09 &#124; NULL &nbsp; &nbsp; &nbsp; &#124;<br/>+----------+-------+---------+------+------------+------------+<br/><br/>为了找出包含正好5个字符的名字，使用“_”模式字符： <br/><br/>mysql&gt; SELECT * FROM pet WHERE name LIKE &quot;_____&quot;;<br/>+-------+--------+---------+------+------------+-------+<br/>&#124; name &nbsp;&#124; owner &nbsp;&#124; species &#124; sex &nbsp;&#124; birth &nbsp; &nbsp; &nbsp;&#124; death &#124;<br/>+-------+--------+---------+------+------------+-------+<br/>&#124; Claws &#124; Gwen &nbsp; &#124; cat &nbsp; &nbsp; &#124; m &nbsp; &nbsp;&#124; 1994-03-17 &#124; NULL &nbsp;&#124;<br/>&#124; Buffy &#124; Harold &#124; dog &nbsp; &nbsp; &#124; f &nbsp; &nbsp;&#124; 1989-05-13 &#124; NULL &nbsp;&#124;<br/>+-------+--------+---------+------+------------+-------+<br/><br/>由MySQL提供的模式匹配的其他类型是使用扩展正则表达式。当你对这类模式进行匹配测试时，使用<br/><br/>REGEXP和NOT REGEXP操作符(或RLIKE和NOT RLIKE，它们是同义词)。 <br/><br/>扩展正则表达式的一些字符是： <br/><br/>“.”匹配任何单个的字符。 <br/>一个字符类“[...]”匹配在方括号内的任何字符。例如，“[abc]”匹配“a”、“b”或“c”。 <br/>为了命名字符的一个范围，使用一个“-”。“[a-z]”匹配任何小写字母，而“[0-9]”匹配任 <br/>何数字。 <br/>“ * ”匹配零个或多个在它前面的东西。例如，“x*”匹配任何数量的“x”字符，“[0-9]*” <br/>匹配的任何数量的数字，而“.*”匹配任何数量的任何东西。 <br/>正则表达式是区分大小写的，但是如果你希望，你能使用一个字符类匹配两种写法。例如， <br/>“[aA]”匹配小写或大写的“a”而“[a-zA-Z]”匹配两种写法的任何字母。 <br/>如果它出现在被测试值的任何地方，模式就匹配(只要他们匹配整个值，SQL模式匹配)。 <br/>为了定位一个模式以便它必须匹配被测试值的开始或结尾，在模式开始处使用“^”或在模式的 <br/>结尾用“$”。 <br/>为了说明扩展正则表达式如何工作，上面所示的LIKE查询在下面使用REGEXP重写： <br/><br/>为了找出以“b”开头的名字，使用“^”匹配名字的开始并且“[bB]”匹配小写或大写的“b”： <br/><br/>mysql&gt; SELECT * FROM pet WHERE name REGEXP &quot;^[bB]&quot;;<br/>+--------+--------+---------+------+------------+------------+<br/>&#124; name &nbsp; &#124; owner &nbsp;&#124; species &#124; sex &nbsp;&#124; birth &nbsp; &nbsp; &nbsp;&#124; death &nbsp; &nbsp; &nbsp;&#124;<br/>+--------+--------+---------+------+------------+------------+<br/>&#124; Buffy &nbsp;&#124; Harold &#124; dog &nbsp; &nbsp; &#124; f &nbsp; &nbsp;&#124; 1989-05-13 &#124; NULL &nbsp; &nbsp; &nbsp; &#124;<br/>&#124; Bowser &#124; Diane &nbsp;&#124; dog &nbsp; &nbsp; &#124; m &nbsp; &nbsp;&#124; 1989-08-31 &#124; 1995-07-29 &#124;<br/>+--------+--------+---------+------+------------+------------+<br/><br/>为了找出以“fy”结尾的名字，使用“$”匹配名字的结尾： <br/><br/>mysql&gt; SELECT * FROM pet WHERE name REGEXP &quot;fy$&quot;;<br/>+--------+--------+---------+------+------------+-------+<br/>&#124; name &nbsp; &#124; owner &nbsp;&#124; species &#124; sex &nbsp;&#124; birth &nbsp; &nbsp; &nbsp;&#124; death &#124;<br/>+--------+--------+---------+------+------------+-------+<br/>&#124; Fluffy &#124; Harold &#124; cat &nbsp; &nbsp; &#124; f &nbsp; &nbsp;&#124; 1993-02-04 &#124; NULL &nbsp;&#124;<br/>&#124; Buffy &nbsp;&#124; Harold &#124; dog &nbsp; &nbsp; &#124; f &nbsp; &nbsp;&#124; 1989-05-13 &#124; NULL &nbsp;&#124;<br/>+--------+--------+---------+------+------------+-------+<br/><br/>为了找出包含一个“w”的名字，使用“[wW]”匹配小写或大写的“w”： <br/><br/>mysql&gt; SELECT * FROM pet WHERE name REGEXP &quot;[wW]&quot;;<br/>+----------+-------+---------+------+------------+------------+<br/>&#124; name &nbsp; &nbsp; &#124; owner &#124; species &#124; sex &nbsp;&#124; birth &nbsp; &nbsp; &nbsp;&#124; death &nbsp; &nbsp; &nbsp;&#124;<br/>+----------+-------+---------+------+------------+------------+<br/>&#124; Claws &nbsp; &nbsp;&#124; Gwen &nbsp;&#124; cat &nbsp; &nbsp; &#124; m &nbsp; &nbsp;&#124; 1994-03-17 &#124; NULL &nbsp; &nbsp; &nbsp; &#124;<br/>&#124; Bowser &nbsp; &#124; Diane &#124; dog &nbsp; &nbsp; &#124; m &nbsp; &nbsp;&#124; 1989-08-31 &#124; 1995-07-29 &#124;<br/>&#124; Whistler &#124; Gwen &nbsp;&#124; bird &nbsp; &nbsp;&#124; NULL &#124; 1997-12-09 &#124; NULL &nbsp; &nbsp; &nbsp; &#124;<br/>+----------+-------+---------+------+------------+------------+<br/><br/>既然如果一个正规表达式出现在值的任何地方，其模式匹配了，就不必再先前的查询中在模式的两<br/><br/>方面放置一个通配符以使得它匹配整个值，就像如果你使用了一个SQL模式那样。 <br/><br/>为了找出包含正好5个字符的名字，使用“^”和“$”匹配名字的开始和结尾，和5个“.”实例在<br/><br/>两者之间： <br/><br/>mysql&gt; SELECT * FROM pet WHERE name REGEXP &quot;^.....$&quot;;<br/>+-------+--------+---------+------+------------+-------+<br/>&#124; name &nbsp;&#124; owner &nbsp;&#124; species &#124; sex &nbsp;&#124; birth &nbsp; &nbsp; &nbsp;&#124; death &#124;<br/>+-------+--------+---------+------+------------+-------+<br/>&#124; Claws &#124; Gwen &nbsp; &#124; cat &nbsp; &nbsp; &#124; m &nbsp; &nbsp;&#124; 1994-03-17 &#124; NULL &nbsp;&#124;<br/>&#124; Buffy &#124; Harold &#124; dog &nbsp; &nbsp; &#124; f &nbsp; &nbsp;&#124; 1989-05-13 &#124; NULL &nbsp;&#124;<br/>+-------+--------+---------+------+------------+-------+<br/><br/>你也可以使用“{n}”“重复n次”操作符重写先前的查询： <br/><br/>mysql&gt; SELECT * FROM pet WHERE name REGEXP &quot;^.{5}$&quot;;<br/>+-------+--------+---------+------+------------+-------+<br/>&#124; name &nbsp;&#124; owner &nbsp;&#124; species &#124; sex &nbsp;&#124; birth &nbsp; &nbsp; &nbsp;&#124; death &#124;<br/>+-------+--------+---------+------+------------+-------+<br/>&#124; Claws &#124; Gwen &nbsp; &#124; cat &nbsp; &nbsp; &#124; m &nbsp; &nbsp;&#124; 1994-03-17 &#124; NULL &nbsp;&#124;<br/>&#124; Buffy &#124; Harold &#124; dog &nbsp; &nbsp; &#124; f &nbsp; &nbsp;&#124; 1989-05-13 &#124; NULL &nbsp;&#124;<br/>+-------+--------+---------+------+------------+-------+<br/><br/><br/>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post/147/#blogcomment2716</link>
<title><![CDATA[[评论] php实现模糊查询功能]]></title> 
<author>哈哈哈 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 30 Jan 2007 16:49:14 +0000</pubDate> 
<guid>http://www.dzhope.com/post/147/#blogcomment2716</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post/147/#blogcomment2717</link>
<title><![CDATA[[评论] php实现模糊查询功能]]></title> 
<author>哈哈哈 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 30 Jan 2007 16:50:30 +0000</pubDate> 
<guid>http://www.dzhope.com/post/147/#blogcomment2717</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>