执行数据库查询时,有完整查询和模糊查询之分。 一般模糊语句如下: select 字段 from 表 where 某字段 like 条件 其中关于条件,sql提供了四种匹配模式: %:表示任意0个或多个字符。可匹配任意类型和长度的字符,有些情况下若是中文,请运用两个百分号(%%
执行数据库查询时,有完整查询和模糊查询之分。
一般模糊语句如下:
select 字段 from 表 where 某字段 like 条件
其中关于条件,sql提供了四种匹配模式:
比如 select * from [user] where u_name like '%三%'
将会把u_name为“张三”,“张猫三”、“三脚猫”,“唐三藏”等等有“三”的记录全找出来。
另外,如果须要找出u_name中既有“三”又有“猫”的记录,请运用 and条件
select * from [user] where u_name like '%三%' and u_name like '%猫%'
若运用 select * from [user] where u_name like '%三%猫%'
虽然能搜索出“三脚猫”,但不能搜索出符合条件的“张猫三”。
比如 select * from [user] where u_name like '_三_'
只找出“唐三藏”这样u_name为三个字且中间一个字是“三”的;
再比如 select * from [user] where u_name like '三__';
只找出“三脚猫”这样name为三个字且第一个字是“三”的;
比如 select * from [user] where u_name like '[张李王]三'
将找出“张三”、“李三”、“王三”(而不是“张李王三”);
如 [ ] 内有一系列字符(01234、abcde之类的)则可略写为“0-4”、“a-e”
select * from [user] where u_name like '老[1-9]'
将找出“老1”、“老2”、……、“老9”;
比如 select * from [user] where u_name like '[^张李王]三'
将找出不姓“张”、“李”、“王”的“赵三”、“孙三”等;
select * from [user] where u_name like '老[^1-4]';
将排除“老1”到“老4”,寻找“老5”、“老6”、……
由于通配符的缘故,导致我们查询特殊字符“%”、“_”、“[”的语句不能正常实现,而把特殊字符用“[ ]”括起便可正常查询。据此我们写出以下函数:
function sqlencode(str) str=replace(str,"[","[[]") '此句一定要在最前 str=replace(str,"_","[_]") str=replace(str,"%","[%]") sqlencode=str end function在查询前将待查字符串先经该函数处理即可。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!