|
'====================================================================
# `7 J% C9 ?' a% [* y'= Copyright (c) 2005 Eason Chan All Rights Reserved.2 C" |6 D# M$ k
'=-------------------------------------------------------------------
! U. x( t) o8 n5 j+ l$ d'= 摘 要:格式化搜索字符函数
4 N- f4 o. `2 T: v6 u'=-------------------------------------------------------------------/ `' M8 z* p7 A/ b/ | R8 X6 ^
'= 最后更新:eason007
7 ~8 B5 m6 E2 E1 n/ b& g'= 最后日期:2005-01-21
( v" f/ w1 K( P# p'====================================================================4 h7 v! R7 v4 @" l; u$ d, L
Function MakeSQLQuery(QueryField,QueryStr). c$ s- T% m7 \: [. Z
Dim TagStart,TagEnd
# g& e _2 |) P4 u Dim TempStr,TempArray
& X# ^' H" \, \# Y5 p7 ^ Dim FullQueryStr
* r6 d- ?2 A3 O. p }; |+ J0 T Dim i,Way
L* J. J" ?! ]( C, v% m
& G7 h( @' Y/ C, m, Z '先找引号定界符3 M3 F! ~+ O$ K* m/ Z2 F4 R4 e
Do
1 D% ]( ^, ?1 Y% C7 X TagStart=InStr(QueryStr,"\")
3 O( R5 ?1 \$ T ~- K5 G: g- }+ n If TagStart>0 Then 1 B8 X0 i3 E+ z. m3 i4 J3 h
TagEnd=InStr(TagStart+1,QueryStr,"\")# V- p1 U& z/ t% \( S7 X$ X
( [( }. P+ B! m+ m7 t( Z/ ^
TempStr=Mid(QueryStr,TagStart+1,TagEnd-TagStart-1)
; M6 P, o$ O6 P" T$ h TempStr=Replace(TempStr," ","#")% C G5 z8 s! _ A' ^, ^, l
( T* k5 v, A {- d# L9 M9 P QueryStr=Left(QueryStr,TagStart-1)&TempStr&Right(QueryStr,Len(QueryStr)-TagEnd)
8 N4 D9 [$ D z* n$ d" t# a End If
k( ] w' \% D7 Z* S/ K Loop While TagStart>05 g4 B& X4 u- _3 J0 s o& v- `2 p
. |* b# d. F; ]+ q5 P '处理or定界符0 @+ @+ q6 t0 o$ O* {
QueryStr=Replace(QueryStr,"|"," @")
`* O6 \5 m, `5 v2 i9 _( ?. n '分隔关键字
0 a! ^9 g1 |/ G- Y* c TempArray=Split(QueryStr," ")3 Z Q) T5 K, M& \
3 H" s ^3 r S# {5 d( V For i=0 To UBound(TempArray)8 G; L% d e; u% z8 i
If Left(TempArray(i),1)="@" Then
L' I$ x6 Z; R! m# l- N7 W) q FullQueryStr=FullQueryStr&" Or "&QueryField
2 H H. `4 D, E' n TempArray(i)=Right(TempArray(i),Len(TempArray(i))-1)
" }- d) Y/ s8 m4 g/ K4 |. G Else( k! p( ~7 M" k1 K8 J7 x4 l
FullQueryStr=FullQueryStr&" And "&QueryField; G1 }" L! V: v# Q! j/ q. P* R
End If
5 k' s& D6 f! C0 u' G
0 r+ W" M& A- w2 f0 T1 F If Left(TempArray(i),1)="-" Then
' Q& {7 f) b* V5 L FullQueryStr=FullQueryStr&" Not "
6 |* V6 r2 c" z }3 s; W TempArray(i)=Right(TempArray(i),Len(TempArray(i))-1)- g, {8 F, w) {' l# h2 F
End If( w/ q) Q* a Z% [
, q9 P( ?' P, v FullQueryStr=FullQueryStr&" Like '%"&TempArray(i)&"%'") V9 C- E, E5 }3 V! x. J m
* o! V& p7 U; _0 r% K% }
FullQueryStr=Replace(FullQueryStr,"%$","")
: }* k- q% n/ W" E6 C FullQueryStr=Replace(FullQueryStr,"$%","")0 X& F+ S+ J3 R" V" r- C
FullQueryStr=Replace(FullQueryStr,"#"," ")
+ v' L7 }' Q/ }) e+ z7 }; q: Q8 K Next& E/ s$ d; Z" m. ~* U
$ S: u4 g. p) }& T( f5 U MakeSQLQuery=FullQueryStr H6 V# H# u$ n( v {
End Function6 n- f) J0 A K% {4 t3 [
: v. u1 X* f6 f
- B/ X, Z! H% ~, x* ~# [说明:
6 A Y! B! V. `: Y- q V3 a用于搜索时,用户可进行复杂的查找,当前1。0版本只支持单字段的搜索,有心人士可自行修改为支持多字段,但请保留我的版权信息。
; I' P$ y9 x" y8 C- l如有疏忽之处,还请原谅!" l ^: w) `/ g3 a! Y' o" f
; _6 ], Q2 W- ~/ M& P示例:
! Z8 A+ h# a4 c% t8 G: e( `3 o4 z1、 空格连接=and,如 你好 我要=%你好% and %我要% ! U* @* v F( q- Z: B6 f& e3 g
2、 避免内容包含字符=-,如 你好 -我要=%你好% and not like %我要% 0 O7 |( ~/ ?& g" D/ x* r& W' u6 q
3、 |=or,如 你好|我要=%你好% or %我要%
' t/ f7 h7 E1 J+ s T! \& _4、 词组搜索用双引号包含,如 \i love this game\=%i love this game%,而非=i and love and this and game # F; ^3 }4 N* r; q. U* c
5、 $为定界符,如 $你好=以 你好 开头的字符,你好$=以 你好 结尾的字符 - M( @, \) P8 H; e& y1 i. `
. M" U9 W+ J9 f9 t
组合查询 : l3 u8 S9 T2 E# G. M
如 \i love this game\|-你好=%i love this game% and not like %你好%
+ y% @4 T8 [6 I# b- G: R如 我要$|-$你好=%我要 or not like 你好% / H; h g. X6 |( x5 u% P" c
如 $\i love this game\ $你好$=i love this game% and like 你好
5 R5 T; T7 Y* M" ~: Y, h
( i: w3 b+ j9 t* n调用:& v* C; C0 _% M( s
% p' [# a( x' C- n1 w; R! J; mDim SQL,WSQL6 H* P1 `! z+ W5 h9 C2 H9 b
Dim Keyword,Field; }& {4 V4 k8 K: X0 M0 ~: ]! j
# U+ C% C7 A" }8 l% ~'get keyword% G* u: c; e) a5 [
Keyword=Request("keyword")
1 s( P6 f+ [' XField=Request("field")
2 Y/ ]. ?1 ^5 f1 {9 V! |2 d
0 g3 @( d4 ^/ P, x3 n- XWSQL=MakeSQLQuery(Field,Keyword)
) J! ~- t& R/ D. M4 p# G
7 l. g. k6 H2 n9 zSQL="SELECT * FROM table1 WHERE 1=1 "&WSQL |
|