|
'====================================================================
& a. k( U* B4 ?0 d# k$ b( n'= Copyright (c) 2005 Eason Chan All Rights Reserved.7 g7 s' ~; P& c8 K; f0 ` e
'=-------------------------------------------------------------------
R' ]" n+ R- B, ^'= 摘 要:格式化搜索字符函数
% l' E% `1 \- D7 K'=-------------------------------------------------------------------$ @9 ?5 @$ h' a# O' z
'= 最后更新:eason007
/ j! ]7 R; j* n! f'= 最后日期:2005-01-213 } O, e, F b7 r
'====================================================================0 [7 ~( y v ~6 v) j
Function MakeSQLQuery(QueryField,QueryStr)
$ e* g+ L1 H) q* b1 A Dim TagStart,TagEnd
# M" @% ]6 _: c5 j) z5 y+ w5 t Dim TempStr,TempArray3 D. A$ {/ z% N. n. N7 x
Dim FullQueryStr
. _/ a) L( U# P. @ Dim i,Way g; `- N$ z) K0 V3 T' W0 N
, \2 ?: [1 w/ V7 i* z1 a) Z
'先找引号定界符
! D6 z0 z/ E6 R# X Do- `2 T8 O0 P( X
TagStart=InStr(QueryStr,"\")7 i- ^( a) @! f6 ^- d
If TagStart>0 Then 9 T; W% K+ n/ _1 K1 B' h) x9 I
TagEnd=InStr(TagStart+1,QueryStr,"\")
6 K) _0 q( o. J" F* X( m; ] 1 W$ T' [6 b2 S; ]' C2 d
TempStr=Mid(QueryStr,TagStart+1,TagEnd-TagStart-1)
8 M, t1 T& E2 R$ V" I" T# g4 D TempStr=Replace(TempStr," ","#")
) ]0 s: S7 L; X; m0 ` V5 I) Z4 H: c2 k
QueryStr=Left(QueryStr,TagStart-1)&TempStr&Right(QueryStr,Len(QueryStr)-TagEnd). w3 F t4 J. r$ P' w5 a
End If; g0 M0 U2 o2 N" M' u; @
Loop While TagStart>0# u1 k5 [2 t& `. _- {% }. z
$ K! c) v9 O1 P7 b& C* F* B6 b
'处理or定界符
% `' w+ k* \3 d% g QueryStr=Replace(QueryStr,"|"," @")
) S, `4 a& o4 w* M. G6 t '分隔关键字
" r2 \' C% E( o5 I: O TempArray=Split(QueryStr," ")
3 b2 _6 J, K% ~, R( a$ W2 A
# U. p' R0 b( D2 W" ~+ X For i=0 To UBound(TempArray)9 a- z9 ~. R* u0 S f
If Left(TempArray(i),1)="@" Then
7 w9 o+ q0 F- C9 o, i* U: ^ FullQueryStr=FullQueryStr&" Or "&QueryField
+ U. R# F% o( I9 X: ~ TempArray(i)=Right(TempArray(i),Len(TempArray(i))-1): t* l( S; |; N3 S
Else% d0 ]9 h" H7 w- c% I* e! k
FullQueryStr=FullQueryStr&" And "&QueryField% f" ]( _& m! m/ b; W _
End If6 X- O, \! {# F# v3 |
7 {9 Q$ k, w& q2 c5 N. N& X
If Left(TempArray(i),1)="-" Then * x" ], C9 `# W$ j. Y5 u
FullQueryStr=FullQueryStr&" Not "
! C+ O& D! |8 s3 O9 v# ^/ Q TempArray(i)=Right(TempArray(i),Len(TempArray(i))-1)
: N7 {# T6 i# u# Y) S! Y' ?3 h' Q End If7 I0 H! Q2 p7 v
- f/ Y. D8 i* _
FullQueryStr=FullQueryStr&" Like '%"&TempArray(i)&"%'"
" w! F: t! ?0 a; i 7 H* y7 L5 a/ ]
FullQueryStr=Replace(FullQueryStr,"%$","")! F- }: k2 N# V# [) ?5 X
FullQueryStr=Replace(FullQueryStr,"$%","")
2 {% a! q f- E6 e2 V$ X; a3 n$ T FullQueryStr=Replace(FullQueryStr,"#"," ")3 H1 P' U- ?' d+ P4 g; n
Next) U$ E7 r# ?; g _. [
6 \& n5 Q, \$ v9 `2 c! S, e MakeSQLQuery=FullQueryStr, j K( P' Y5 W5 k& E# ]$ Q( X
End Function! c7 U6 }+ Y+ ^7 C$ r+ }1 |
' U1 O m( g0 j3 ~. X+ |7 h1 b& S5 q' D
' V. i: B8 ~0 g G说明:/ e+ b4 O% b" U4 L: o5 U$ C
用于搜索时,用户可进行复杂的查找,当前1。0版本只支持单字段的搜索,有心人士可自行修改为支持多字段,但请保留我的版权信息。) Z. q0 V* p! b$ [. n- f8 Z
如有疏忽之处,还请原谅!
" Y5 k) R7 Z& ]5 @3 ?5 K h% v! r& f3 \. o
示例:& w* ]* e6 M/ T0 M. z+ B- N
1、 空格连接=and,如 你好 我要=%你好% and %我要%
5 }4 {( y, x6 C n4 s2、 避免内容包含字符=-,如 你好 -我要=%你好% and not like %我要%
7 l/ B8 j" M# j3、 |=or,如 你好|我要=%你好% or %我要% ) _ y& ?. P% t" E( |
4、 词组搜索用双引号包含,如 \i love this game\=%i love this game%,而非=i and love and this and game * q: ^2 o/ a P" j8 M
5、 $为定界符,如 $你好=以 你好 开头的字符,你好$=以 你好 结尾的字符 6 O1 b7 M8 x% f Y
6 h% c' L& p+ L; U; d' P4 [- ?组合查询
- U. |( }: h3 L5 V) {+ d1 H如 \i love this game\|-你好=%i love this game% and not like %你好%
1 F# Y+ L% u5 ~. q+ m0 ^% y$ \9 |% M如 我要$|-$你好=%我要 or not like 你好% / |7 G' `# L+ L, s* N+ r
如 $\i love this game\ $你好$=i love this game% and like 你好 , [% }3 _8 |' L: f
2 q( q) N& O1 j, Y调用:/ i2 V7 y# U( ], z, M: k" q2 w
- A& ]- R+ S4 G$ ^6 j1 yDim SQL,WSQL
- }9 `: J9 U5 l1 i/ \% SDim Keyword,Field
8 z8 Z$ R5 w0 K3 |# Q- C
0 x5 \6 n, d8 ]1 w" N/ Y'get keyword8 r* C; I9 m; V% H7 {
Keyword=Request("keyword"): I3 D0 k9 ]& O- P" I
Field=Request("field")9 U. h5 s& V. s+ M2 S5 G
, T. ^3 A. P' e$ n5 q3 ~
WSQL=MakeSQLQuery(Field,Keyword)& l0 J0 V/ l8 J6 l6 w( p8 d- i7 p
* r* r4 O8 ~: }
SQL="SELECT * FROM table1 WHERE 1=1 "&WSQL |
|