|
'====================================================================
& f/ K ~: o9 K3 i'= Copyright (c) 2005 Eason Chan All Rights Reserved.
$ k3 l3 @- ], M, O# i'=-------------------------------------------------------------------
_' G* g5 P: V! c5 J1 H'= 摘 要:格式化搜索字符函数
$ }/ D% e) g' C+ x# ?'=-------------------------------------------------------------------+ i; D! a5 M5 r+ H/ x; A
'= 最后更新:eason0077 k' o# L( l' ]% a7 |. }
'= 最后日期:2005-01-21( {* d. `% @7 C; H
'====================================================================
. Q& F: y1 w1 [9 u1 q& h; tFunction MakeSQLQuery(QueryField,QueryStr)
& i5 l& X! n2 ~* q* z& \ Dim TagStart,TagEnd' a4 p' j2 [9 U- N! X7 O6 @
Dim TempStr,TempArray
1 p+ G! y8 k' v Dim FullQueryStr4 F( }" ]' U% j$ u O* o; @
Dim i,Way
( f: u( w! t) ^/ ?' D - u2 \9 T* R1 n9 X
'先找引号定界符6 N3 S) |0 p+ m7 G: H4 a- e5 E
Do1 V! m& r; e1 m- d; [3 B) O3 C
TagStart=InStr(QueryStr,"\")+ W( J% l0 h+ q0 n: {
If TagStart>0 Then 1 d5 x Q2 w! {+ K8 L3 G+ w
TagEnd=InStr(TagStart+1,QueryStr,"\")/ i/ Y( f1 Q+ @/ [- O3 a
" v9 s0 Q Q7 U+ K+ y TempStr=Mid(QueryStr,TagStart+1,TagEnd-TagStart-1)
5 p0 P( w# r+ t TempStr=Replace(TempStr," ","#")# c1 f2 Z2 C G& B+ J* y6 [% q
, J7 k6 B# J! s+ p; I) `
QueryStr=Left(QueryStr,TagStart-1)&TempStr&Right(QueryStr,Len(QueryStr)-TagEnd)
5 ~0 [7 j/ @; W$ x% |& g# D End If
6 G9 I: G5 }" U+ T' o9 g Loop While TagStart>02 @5 E o' v4 Y
& z% h1 G7 V9 n( H3 Z '处理or定界符
1 d* F- c/ _, o) X% \1 Q" b QueryStr=Replace(QueryStr,"|"," @")" B6 A% ?8 h/ O0 [* ?8 n
'分隔关键字
8 a0 w0 B* ?! j( e# c TempArray=Split(QueryStr," ")$ T* W* {! z. e2 V3 k* W+ Q3 j
9 D: P6 y: s5 z' _0 w& } For i=0 To UBound(TempArray)
7 J7 E: B8 F3 L7 Q+ r' A If Left(TempArray(i),1)="@" Then
2 M5 c! X6 ]# a: S$ p3 }) g FullQueryStr=FullQueryStr&" Or "&QueryField
8 k7 z6 ]! e' g) a( M TempArray(i)=Right(TempArray(i),Len(TempArray(i))-1)
1 i+ j" ?; y: Y5 i; U) E2 n Else
: b; p" [9 s) d# ]+ { FullQueryStr=FullQueryStr&" And "&QueryField
, R. {6 t; H0 j3 }* d8 n( `1 e End If
' v: q7 m& ~8 _! l; M, f" u
- N0 h# ?2 y- a: ^" U If Left(TempArray(i),1)="-" Then 3 M+ j& ^2 |0 u* J8 h
FullQueryStr=FullQueryStr&" Not "
/ Q1 s/ K" A+ J* D: N W4 Q$ b* M TempArray(i)=Right(TempArray(i),Len(TempArray(i))-1)
) w6 ^. K) `2 w1 Z$ y End If
6 h7 E0 j' \7 @, M! |$ }2 G% J
/ D t, o M& f: |3 M8 i' K FullQueryStr=FullQueryStr&" Like '%"&TempArray(i)&"%'"( Z2 P5 H; w# e1 M) @. M
3 t9 h! X) V3 ]7 |1 }! j' v. w
FullQueryStr=Replace(FullQueryStr,"%$","")
) H+ g, g2 b7 D. b# o9 ] FullQueryStr=Replace(FullQueryStr,"$%",""); V5 F0 O T% L
FullQueryStr=Replace(FullQueryStr,"#"," ")* z$ O q' m' }* B# d8 _
Next
, u7 Z& _/ S3 e0 r) r" Y
6 ]4 {6 ^ `0 X MakeSQLQuery=FullQueryStr
9 ~7 t" L8 b9 S3 ^/ nEnd Function+ T- H5 t& a2 h w+ `
+ @# d8 u J* r2 Y2 X
( L# N/ l/ x8 m# R- V说明:; K; \. S9 u7 u/ F
用于搜索时,用户可进行复杂的查找,当前1。0版本只支持单字段的搜索,有心人士可自行修改为支持多字段,但请保留我的版权信息。
! d$ x9 k1 L+ G8 D& p如有疏忽之处,还请原谅!& w0 n& J( E4 }1 T0 t6 C
5 a! }3 O/ j3 d* H5 ?1 ~0 |( x
示例:( z: J1 k k( y: }# l. O
1、 空格连接=and,如 你好 我要=%你好% and %我要%
$ w. x2 _( [9 t+ L3 ^2、 避免内容包含字符=-,如 你好 -我要=%你好% and not like %我要% - \/ q& l2 I+ X* W1 C$ l
3、 |=or,如 你好|我要=%你好% or %我要%
9 Q% {& _1 E- s3 A- P7 ~4、 词组搜索用双引号包含,如 \i love this game\=%i love this game%,而非=i and love and this and game
, C2 i( T6 ~' O# R2 T5、 $为定界符,如 $你好=以 你好 开头的字符,你好$=以 你好 结尾的字符
+ L0 Q4 f8 N' [, v/ f% U; d8 A& f& ?. g
组合查询
) J9 j& V& a# S: F如 \i love this game\|-你好=%i love this game% and not like %你好%
( s1 {& H2 R* ^/ o4 k4 }* ?; R) `如 我要$|-$你好=%我要 or not like 你好% 7 Z3 o0 O& O. c' R# J! v
如 $\i love this game\ $你好$=i love this game% and like 你好
% K* a9 \% V" s$ X3 G
3 N/ o3 A$ a4 q1 {) D. K$ @3 d调用:% ?3 a9 f5 b) p/ F" t, X
4 m% b6 W; l2 V9 o. X& l7 H0 o; i( j
Dim SQL,WSQL
* q6 K/ w- `9 ?1 }Dim Keyword,Field& ]4 S& X5 a5 a. P4 V) ]+ K! |/ a
- n$ b2 C+ p1 X7 d
'get keyword: Q$ T- q- }# _' |: }
Keyword=Request("keyword")
& E1 x& K) y! b$ FField=Request("field")
6 @4 o& Q& {8 e1 T. \( m1 [. }
- M5 y+ P& }" q5 xWSQL=MakeSQLQuery(Field,Keyword)
! r' N% Q; V2 r% Y5 u5 w
1 G4 I- h9 W3 `3 F4 p1 D0 cSQL="SELECT * FROM table1 WHERE 1=1 "&WSQL |
|