|
'==================================================================== X9 f! U* m/ x" |- r, V6 o* H2 a
'= Copyright (c) 2005 Eason Chan All Rights Reserved.
7 ~! U, w7 }/ J( w'=-------------------------------------------------------------------4 @/ E& O: C. n x* V, i0 Z
'= 摘 要:格式化搜索字符函数0 m6 F& Z5 E: d' j7 g
'=-------------------------------------------------------------------1 \: a, @% m8 `
'= 最后更新:eason007
5 l: a% V4 x, X; U) V2 z6 H'= 最后日期:2005-01-21; `8 Y4 x- }% r" o1 n& o
'====================================================================
9 L. S+ Z3 G6 c9 T8 `Function MakeSQLQuery(QueryField,QueryStr)
$ b6 c: r3 d% E, s+ d! t Dim TagStart,TagEnd9 Z" |6 o2 Q/ H7 W
Dim TempStr,TempArray
6 {; E( O9 m9 `2 ?! l4 \* O m$ ] Dim FullQueryStr
2 H/ B/ _6 Q2 F7 y; c Dim i,Way7 a( x) t( M6 ^
( P$ S; o& ^: ~' p$ Q, G( g% Z '先找引号定界符
' d2 g% j0 r; B* i+ p Do- r1 ?% t i1 }8 M1 p8 ~, i+ b' P
TagStart=InStr(QueryStr,"\")) U6 H! D4 d4 N# B
If TagStart>0 Then
, o; n8 P. S, B& L' S ^* O TagEnd=InStr(TagStart+1,QueryStr,"\")4 l' k" O4 z7 m# ]/ O
) E# o$ m3 A5 U# D TempStr=Mid(QueryStr,TagStart+1,TagEnd-TagStart-1)
6 X0 k T/ V% r; } H4 w+ g1 e+ D TempStr=Replace(TempStr," ","#")$ {$ d6 _- a4 J( A
* i; x- G9 O4 {& y QueryStr=Left(QueryStr,TagStart-1)&TempStr&Right(QueryStr,Len(QueryStr)-TagEnd)
6 A+ Y% _0 F J; S7 M; E End If
% d' h2 I. i* h3 _0 l" R) u! ? d Loop While TagStart>01 S* Z$ }, m- ~9 O p4 Z
+ Y5 `0 U4 c+ `! C i7 Z' W
'处理or定界符
6 T1 s+ [# Z* d2 e a; ~6 k QueryStr=Replace(QueryStr,"|"," @")/ o/ U( J, W- h( U- `( c# b# \
'分隔关键字
( ~% {& ~1 c$ u8 X* q TempArray=Split(QueryStr," ")& }% U" U" g+ w0 i, W$ j$ [$ i
, W! m% `6 W& A t1 ~
For i=0 To UBound(TempArray)0 j+ R1 |/ B+ I$ r0 T
If Left(TempArray(i),1)="@" Then
4 h# s( O/ Q2 _2 M) n3 y FullQueryStr=FullQueryStr&" Or "&QueryField0 g8 t; |7 I6 U \) G# ?4 E
TempArray(i)=Right(TempArray(i),Len(TempArray(i))-1)" z) x0 q# Z) r* A w& M8 W, a
Else
' Y+ }6 @$ _% ~ FullQueryStr=FullQueryStr&" And "&QueryField% r4 t# O( E$ a$ O, ?
End If' A! f: x! y! G* y+ V# Y4 Q
2 b8 K" i% _4 I( b/ E: A$ D
If Left(TempArray(i),1)="-" Then
0 |9 t0 ?5 f+ Y- |' D2 M1 Q FullQueryStr=FullQueryStr&" Not "
$ _# S+ K% N* T- E; O, d' p TempArray(i)=Right(TempArray(i),Len(TempArray(i))-1)
: B8 Z' c( }8 W4 Z2 p9 ^3 W End If
0 m" W7 ?# S2 E- \: i 5 W+ h2 r, v* f
FullQueryStr=FullQueryStr&" Like '%"&TempArray(i)&"%'"
4 G+ [8 P2 F: q+ j. ?, r; u* i # U* X; @, e- j9 A: P4 o
FullQueryStr=Replace(FullQueryStr,"%$","")2 \/ u ]% |8 t0 G4 C- I
FullQueryStr=Replace(FullQueryStr,"$%","")
- R5 V7 j; p4 n' R0 u: ^) {+ V. i- p FullQueryStr=Replace(FullQueryStr,"#"," ")# y! y* l6 c6 H% F' W" F$ {- h
Next
+ m. }: Q+ g+ O8 Z$ f+ h ! t7 _% a$ A' J+ J' e" U( x
MakeSQLQuery=FullQueryStr% \/ b9 w3 F" y" `& ]
End Function
0 ?) j2 ]& ]$ a2 F5 g* H" j2 J5 S0 t- a: E' A9 B3 R5 S
0 O5 Z" p1 e9 d) O! q7 F说明:
8 S- ]& v5 `8 N4 H6 s U' ?用于搜索时,用户可进行复杂的查找,当前1。0版本只支持单字段的搜索,有心人士可自行修改为支持多字段,但请保留我的版权信息。
- W" c1 Q" E; \% p' |6 [4 N' S, b' U如有疏忽之处,还请原谅!# c! l: ~5 ~( f8 r
' }. _. w7 c4 O8 [& K1 g
示例:" m. `( L& d) H5 D: c
1、 空格连接=and,如 你好 我要=%你好% and %我要% 8 z' Q# g# G2 e; `! d- S2 W% q
2、 避免内容包含字符=-,如 你好 -我要=%你好% and not like %我要%
) \/ f4 r. Q7 e7 ^' e6 o# f$ z3、 |=or,如 你好|我要=%你好% or %我要% 1 b3 W: B7 K7 b/ c# L9 u
4、 词组搜索用双引号包含,如 \i love this game\=%i love this game%,而非=i and love and this and game
( d" H( B/ p0 `, e! `5、 $为定界符,如 $你好=以 你好 开头的字符,你好$=以 你好 结尾的字符 + k- p, K1 |4 m% O2 v2 J
. H, d0 O: Y, V
组合查询 ' R! Q7 ^3 ]! H, m# S
如 \i love this game\|-你好=%i love this game% and not like %你好%
7 k7 R, f! t) S& u E* `: u如 我要$|-$你好=%我要 or not like 你好%
5 V2 }9 e$ p A& Y$ @" T( h( \如 $\i love this game\ $你好$=i love this game% and like 你好
" _2 F& ?' W9 h
4 y. h: q6 K9 {/ G: H& m4 h( i调用:' Y) k- j- \# F& D6 `" h
% j' [0 w5 x' a- F# y2 h
Dim SQL,WSQL4 y& K& i' v! P4 |
Dim Keyword,Field
% P$ {! A, R e' ~) p# V. ~* e( c5 s( L% G$ o
'get keyword( {- h/ r6 O. r J* E
Keyword=Request("keyword")$ E4 W( c. C' m7 i, Q; l. R
Field=Request("field")5 l- ~$ W& b) V7 o6 s
+ Y9 l9 I- } ? x& Z% |( l- M2 ^WSQL=MakeSQLQuery(Field,Keyword)
* J* \: Q8 M! C& _
) m5 {# P4 p- I. ?SQL="SELECT * FROM table1 WHERE 1=1 "&WSQL |
|