|
'====================================================================; w( M0 V1 z6 K7 D
'= Copyright (c) 2005 Eason Chan All Rights Reserved.- o8 U- {0 G1 d f
'=-------------------------------------------------------------------
+ {7 q7 n1 |& b% o1 p'= 摘 要:格式化搜索字符函数
9 s& C+ C( }* y. B4 d2 b'=-------------------------------------------------------------------; K5 [0 W% G4 g5 f2 s
'= 最后更新:eason007, | L! C4 p/ Q# K: v/ }* j
'= 最后日期:2005-01-21
* F, B" z0 o% n' r# }7 h'====================================================================
$ U/ X% G$ Q& L6 GFunction MakeSQLQuery(QueryField,QueryStr)
; D2 I/ |1 q* B% Z# w Dim TagStart,TagEnd2 s8 c6 P; c+ n- [
Dim TempStr,TempArray- k5 @$ y: y d% o% ]
Dim FullQueryStr$ r6 S+ _7 F/ }! _% \+ i @6 [
Dim i,Way
/ e+ H3 w: y- {; h/ J
* \! g& o+ } W9 e7 Y$ o+ V' J '先找引号定界符' T* c% p+ P/ J' [% U
Do
. k$ A' U& Y) t1 y4 o$ k+ b TagStart=InStr(QueryStr,"\")
3 ?9 U( J! g1 j6 H5 h If TagStart>0 Then
3 o! t6 s! O+ |" l* ~' s TagEnd=InStr(TagStart+1,QueryStr,"\")7 u- p% ~& w8 Q, F
) v( o( X' v. H3 a1 ~. n TempStr=Mid(QueryStr,TagStart+1,TagEnd-TagStart-1)
/ n, E: n2 A7 C2 w; M* \: G$ }' o TempStr=Replace(TempStr," ","#")) p, ^( x, n4 t4 C2 C
2 |8 z: ]2 w/ D8 T8 { QueryStr=Left(QueryStr,TagStart-1)&TempStr&Right(QueryStr,Len(QueryStr)-TagEnd)% x! c0 {% ]. d9 ~# j$ k% P
End If8 Z- r2 i& C) Y P) l% a
Loop While TagStart>0: f( [4 K; o8 V
; E9 h5 E" K1 [0 ? '处理or定界符: S' W. d3 j" q; v; M9 A5 Q( c7 [0 A
QueryStr=Replace(QueryStr,"|"," @") \; |0 b5 b! D5 t
'分隔关键字
/ j. g3 s3 v ]$ I. Q TempArray=Split(QueryStr," ")
: h, b W) |+ w! n( L 6 Q! M3 w; n ~# U3 p9 Q. H
For i=0 To UBound(TempArray)
* X$ D% I% ~5 @ If Left(TempArray(i),1)="@" Then% r9 Y- R: ~7 G; O0 H5 y: N
FullQueryStr=FullQueryStr&" Or "&QueryField+ W* A* ~1 G+ I; }
TempArray(i)=Right(TempArray(i),Len(TempArray(i))-1)
% @1 W: z4 y( B- I/ z- D6 W3 M Else
3 [; j2 q6 K" h) r% N FullQueryStr=FullQueryStr&" And "&QueryField
5 Y& k8 \0 x F- ?" r1 Q _4 m N End If
% P& W% G: S' E% Y 0 C9 U7 y) V; x4 Y# ?. F5 U2 p
If Left(TempArray(i),1)="-" Then
! U6 y$ i+ r" `: v5 ~/ r FullQueryStr=FullQueryStr&" Not "
" a& }' i1 s, P/ d- f ? TempArray(i)=Right(TempArray(i),Len(TempArray(i))-1)
7 W$ ^9 B6 R6 a9 u End If; K- i4 q+ }& a2 t3 ] }
! D0 Y6 f8 U6 r# b FullQueryStr=FullQueryStr&" Like '%"&TempArray(i)&"%'"9 I* i; {& v9 e" v
0 H( Q- ^& S3 v' v+ v5 ?( h7 M FullQueryStr=Replace(FullQueryStr,"%$","")
2 q: _* r: N8 J1 p4 l# k3 O" r! P& @' H FullQueryStr=Replace(FullQueryStr,"$%","")# |" n5 F4 C" d, T/ z; d
FullQueryStr=Replace(FullQueryStr,"#"," "); L% p1 U- @; N- A! l% o* I
Next* x: q: ?" F. u5 }7 g
D' H3 Y0 p9 S3 a7 N [
MakeSQLQuery=FullQueryStr& H+ i* R8 C& p$ L" J3 [: v
End Function" k' H, v9 g. R9 X- x5 d
6 B3 O4 y0 q# c9 l8 O/ j. l
# S5 G: E' [& b3 _% d1 [
说明:
7 ]8 X: e4 O( e& H* @% C9 r5 ^: B用于搜索时,用户可进行复杂的查找,当前1。0版本只支持单字段的搜索,有心人士可自行修改为支持多字段,但请保留我的版权信息。
1 K* x* D; G! G1 I3 K& |. Y如有疏忽之处,还请原谅!
0 K( B* o- y( |) ?; f' ~7 A
3 I! h X+ W6 H示例:) ]! X- r& _2 q7 \% z5 \
1、 空格连接=and,如 你好 我要=%你好% and %我要% ) Z# X$ I4 o$ N3 H/ P# V
2、 避免内容包含字符=-,如 你好 -我要=%你好% and not like %我要% ! Z6 ]" J4 `- }/ R; }
3、 |=or,如 你好|我要=%你好% or %我要%
7 l$ O Y) z, x4、 词组搜索用双引号包含,如 \i love this game\=%i love this game%,而非=i and love and this and game
6 _7 b3 p3 }) o' m5、 $为定界符,如 $你好=以 你好 开头的字符,你好$=以 你好 结尾的字符 ( G3 i' P" d$ n
9 m# `( q1 L' h! Z3 q5 [
组合查询
: O) n4 b; W- L, a* {( x如 \i love this game\|-你好=%i love this game% and not like %你好%
# V3 K9 q* D' |) s2 i如 我要$|-$你好=%我要 or not like 你好%
& n% T" g- y) e2 t* u( u$ P" g如 $\i love this game\ $你好$=i love this game% and like 你好
' y, k8 H: k0 q4 \4 _0 P: I. K; a. G: O" k
调用:
8 r5 m7 w; g: q
6 ]* u1 a5 m& b8 k9 c5 ~Dim SQL,WSQL4 @3 W& g6 t3 I( d7 f& w8 j
Dim Keyword,Field
" p" w, I% J; r7 }
+ t, L& v5 R4 n2 Z$ {'get keyword
3 Q0 ^: n( A/ f. aKeyword=Request("keyword")
7 b" k5 h) Y- s4 `Field=Request("field")! [; Y& n$ r% H; t R# y# ?7 b
: ?7 F, g, C! ]) S! b
WSQL=MakeSQLQuery(Field,Keyword)
7 \' b! m7 y9 k9 A6 m
; B! P3 H2 n. q) TSQL="SELECT * FROM table1 WHERE 1=1 "&WSQL |
|