|
'====================================================================
4 `! {: c6 l$ T$ |& u'= Copyright (c) 2005 Eason Chan All Rights Reserved.1 D. ^ H$ R' s% n" u: g7 q- l
'=-------------------------------------------------------------------
7 t3 l# \$ m" `2 M7 K; d& @) l'= 摘 要:格式化搜索字符函数
5 ]9 @" ] m0 [# \'=-------------------------------------------------------------------
" q2 U4 N* e( G- f- S0 T& S% @& \7 k! |'= 最后更新:eason0072 r. ?% @0 ^$ H- h( ~) N/ J
'= 最后日期:2005-01-21$ T; ]5 c6 k- z: w h
'====================================================================
7 p, M' F; u( S" ^5 S3 x* }( JFunction MakeSQLQuery(QueryField,QueryStr): c i$ F# Q0 c
Dim TagStart,TagEnd
5 R" d( F! f1 _9 \ Dim TempStr,TempArray
6 k, }6 I) x3 A: P2 y Dim FullQueryStr
, C2 w' N/ B" ^ Dim i,Way( f# P* {& Z; N: r
; E: |; {. L% Y8 C0 }/ q5 P '先找引号定界符7 C) C1 g6 g7 Q4 u# l; `) ^* R s
Do
# t& y% b& l7 [+ {7 S3 z4 q TagStart=InStr(QueryStr,"\")
& [3 `( z1 ~9 S0 R If TagStart>0 Then
; v. U+ @# Z8 w# P! d/ T TagEnd=InStr(TagStart+1,QueryStr,"\")" c3 K S+ i8 y+ e8 U, f2 w
" x; C# O; j6 b8 d
TempStr=Mid(QueryStr,TagStart+1,TagEnd-TagStart-1)
* i$ p* B6 P/ x# |8 S+ J# z TempStr=Replace(TempStr," ","#")$ P$ n, d% P2 i6 j- ^, Y
/ d/ G7 A- ]+ @5 _9 Y4 S9 \
QueryStr=Left(QueryStr,TagStart-1)&TempStr&Right(QueryStr,Len(QueryStr)-TagEnd); z4 Z/ }2 I" J- v6 P
End If
0 m5 b8 I& v+ Q, G; F" D" R" g Loop While TagStart>0
" r. x) a/ m5 [% j * H( c2 Q9 Y/ g6 J$ L
'处理or定界符
" P; y7 G+ ~. _+ l3 b. j QueryStr=Replace(QueryStr,"|"," @")
$ f( B2 m+ v7 Z$ r4 M! F '分隔关键字
* s8 C) j! c8 N% @9 [ TempArray=Split(QueryStr," ")+ u. u5 W" }2 e6 f
8 }6 s4 v$ Q3 p4 k For i=0 To UBound(TempArray)
9 R% \) [) J2 n6 W If Left(TempArray(i),1)="@" Then
# m& m* z, W) l" B' C. I! i# u1 c0 a: Q FullQueryStr=FullQueryStr&" Or "&QueryField% A& n5 w; L/ _" ~/ n8 h
TempArray(i)=Right(TempArray(i),Len(TempArray(i))-1)6 t3 }8 |: S6 |' C' f9 ]0 ~+ @9 @ G
Else
3 B: p/ n/ a7 B3 _# S FullQueryStr=FullQueryStr&" And "&QueryField
0 |# ^8 V5 h9 {: ] End If
& J7 o r4 z2 M$ m/ f
, V. t! \$ D" L3 W1 Q6 n If Left(TempArray(i),1)="-" Then 4 y1 } B( {" e
FullQueryStr=FullQueryStr&" Not "5 l) j) [( T$ ]8 {: V) _ `
TempArray(i)=Right(TempArray(i),Len(TempArray(i))-1)
4 @6 x5 ?. u7 S2 J- E End If
+ F) R5 p9 l, |5 l3 T" i/ ] 2 F9 F D' G$ w5 ~
FullQueryStr=FullQueryStr&" Like '%"&TempArray(i)&"%'"
) }- b% r4 N; y2 E & _4 r' i- I: w: N. t# s' {
FullQueryStr=Replace(FullQueryStr,"%$","")0 `1 O) ^$ Q' O+ O5 I) p) {4 I
FullQueryStr=Replace(FullQueryStr,"$%","")
! q9 k. O2 s1 D" T" D+ D8 g FullQueryStr=Replace(FullQueryStr,"#"," ")
& S2 P) J( m2 X2 D& A Next% w% c5 n, P$ i
/ W$ b X9 \6 C; l% Z
MakeSQLQuery=FullQueryStr, k+ h: D) r3 g5 y0 Z1 m1 o9 Z$ P& O( t
End Function
6 @* x6 ?8 M2 k& } X [' M/ L, L% j- R2 P. f$ Q' F+ \
& w! k: H1 j/ r" I3 T9 F说明:1 J6 t, ~( Y b o* m
用于搜索时,用户可进行复杂的查找,当前1。0版本只支持单字段的搜索,有心人士可自行修改为支持多字段,但请保留我的版权信息。
( G* X- ^, a5 q* w5 f1 z5 M( \如有疏忽之处,还请原谅!
* [3 Y. C7 O: e: S. W
. |) S2 G5 F) ?示例:+ c' ]# T, w; Y9 ?
1、 空格连接=and,如 你好 我要=%你好% and %我要% 8 t2 N2 Q7 Q3 d- B; X
2、 避免内容包含字符=-,如 你好 -我要=%你好% and not like %我要% + H" x1 I& F- Y6 C7 O
3、 |=or,如 你好|我要=%你好% or %我要% 6 A; p; h, X1 a1 O9 f& I& z
4、 词组搜索用双引号包含,如 \i love this game\=%i love this game%,而非=i and love and this and game 6 j3 S* k. M* N0 c
5、 $为定界符,如 $你好=以 你好 开头的字符,你好$=以 你好 结尾的字符
- v% k) C$ a% P6 Q6 z* T- L0 D4 z9 Q; C* N1 S9 l$ r3 j! t0 G+ m+ f
组合查询 5 _- Z f) X; p
如 \i love this game\|-你好=%i love this game% and not like %你好%
( M) w0 S( }6 S1 A$ q0 n如 我要$|-$你好=%我要 or not like 你好% 1 v( n( Z0 q ?$ a; C
如 $\i love this game\ $你好$=i love this game% and like 你好 3 J- i. X0 f v
( x6 r. i9 }5 u/ c调用:3 @/ g& x& v" D" b
$ {7 \# K2 i7 |3 R9 O% O
Dim SQL,WSQL6 }) {1 K" y1 l. l/ p& Z4 l
Dim Keyword,Field* e3 D$ H! a2 J
5 t8 i( t: S# W6 P'get keyword
# X" C2 R! Z$ Q. w( dKeyword=Request("keyword")
7 ?: I* \) |9 k) uField=Request("field")
2 ]1 S- d% p/ \. O$ z' Y6 W f. v
0 ~7 c2 x2 Z. U* h) ?& W& W \WSQL=MakeSQLQuery(Field,Keyword)" J$ ?! ^1 N5 S6 S9 `" H
7 n4 n3 ]% l% M4 U% VSQL="SELECT * FROM table1 WHERE 1=1 "&WSQL |
|