|
'====================================================================
$ y# |4 z; Q5 x3 j'= Copyright (c) 2005 Eason Chan All Rights Reserved.
" [: O& D6 S% Y7 @6 U'=-------------------------------------------------------------------) ^* S! E- e1 l' m& p
'= 摘 要:格式化搜索字符函数
$ ]5 f0 {& ]" l* C2 E'=-------------------------------------------------------------------& Q/ o! P4 W1 F; N4 p( A
'= 最后更新:eason007
, b k" T5 v8 b4 h/ i- w'= 最后日期:2005-01-21
J/ I% m7 N E+ l3 z& h'====================================================================
( g% T+ N9 w# Q, G$ K$ U: @Function MakeSQLQuery(QueryField,QueryStr)
% C/ E* j; r( m0 a, P! W4 b+ g Dim TagStart,TagEnd
/ X* Y4 r& T8 a+ p: x, x* r% K Dim TempStr,TempArray! y# Y- _1 P' b9 [0 l* S, W
Dim FullQueryStr" _" i J1 A$ y3 u) B
Dim i,Way
k2 @/ `3 P$ {2 V, ~6 d9 B9 r5 M
* [ P4 U" w: ?5 g; }1 B& R1 A) H '先找引号定界符
8 P. S, k; `. ?! g& Y Do
2 c7 H: v' D, R6 j7 K TagStart=InStr(QueryStr,"\")0 M, U9 o0 G( l$ l: V
If TagStart>0 Then
' {0 z6 Y3 w( T4 |+ g, ] TagEnd=InStr(TagStart+1,QueryStr,"\")" Q$ D* p( A# O) U3 p9 ]
" e3 O( r* k: A
TempStr=Mid(QueryStr,TagStart+1,TagEnd-TagStart-1)
9 D3 K! A; U# v) J) ?' H TempStr=Replace(TempStr," ","#")% M' Q2 T1 U8 M( u
# [4 ^) C. Z1 c) T) I+ K) W5 ^$ `
QueryStr=Left(QueryStr,TagStart-1)&TempStr&Right(QueryStr,Len(QueryStr)-TagEnd)2 | I/ Z. Y& i; h. T# O6 X
End If8 u, l W8 q8 ^8 }3 L! K0 H
Loop While TagStart>0% s! G* l# |# F: S) J' Z0 b
5 |# p' w+ l( b7 A5 { '处理or定界符
4 L4 q( j* s0 @ QueryStr=Replace(QueryStr,"|"," @"). U" J& S- m U) `
'分隔关键字/ r5 F# y% [4 B1 I
TempArray=Split(QueryStr," ")7 h2 n: V% `2 U$ @9 T' L3 k5 m( P
) Q: O$ s% ]" y2 E# l& r For i=0 To UBound(TempArray) A8 S- c7 R) R9 L
If Left(TempArray(i),1)="@" Then
7 L8 r. t. |8 B FullQueryStr=FullQueryStr&" Or "&QueryField/ H; e0 `- J3 f: ]7 [3 r/ \
TempArray(i)=Right(TempArray(i),Len(TempArray(i))-1)
9 I! Z9 t6 s6 }7 b% g% Z% U N Else0 F6 v4 r+ R' O8 p
FullQueryStr=FullQueryStr&" And "&QueryField8 N! V9 j' c- M. V" q- E3 _6 d& d
End If4 T- t, w9 P5 Y4 g7 e$ I. n
- `$ Y, K# k& d, f# x5 J" Y( z If Left(TempArray(i),1)="-" Then 5 ^: I4 l1 Y& B
FullQueryStr=FullQueryStr&" Not "# M" M5 b0 g0 P+ ]
TempArray(i)=Right(TempArray(i),Len(TempArray(i))-1)! d8 l) ]. R" K0 g
End If
" g* h$ I9 H* N1 T9 P$ q 7 z3 l$ d6 s. L/ j' {- p" D& f
FullQueryStr=FullQueryStr&" Like '%"&TempArray(i)&"%'"
* t% J) t6 c' ?# B/ Q0 `
. s- z/ g; S" r+ C: I1 c1 p ? FullQueryStr=Replace(FullQueryStr,"%$","")# |$ }8 E+ `& b. A) [3 Y
FullQueryStr=Replace(FullQueryStr,"$%","")
. o7 U9 T) d8 z# f7 I- o: X FullQueryStr=Replace(FullQueryStr,"#"," ")
+ m m; z: V: L+ D O- E, f2 f3 K Next2 C3 N4 \2 m: Q, z
9 R3 x9 H) U/ C MakeSQLQuery=FullQueryStr- _' b. L( Z; n3 h- Y
End Function+ F8 B3 f5 J, n/ {! B
3 v* U/ a1 n% @! x2 m% P/ u
8 n$ ^$ @) p: k# [说明:
$ g7 o8 D7 l% P# `+ V2 J用于搜索时,用户可进行复杂的查找,当前1。0版本只支持单字段的搜索,有心人士可自行修改为支持多字段,但请保留我的版权信息。$ M# g- }8 Q$ c6 Y( c
如有疏忽之处,还请原谅!
9 s% {5 O1 n# b) p4 m" {. U( `/ j. q& W8 h
示例:5 S. {% W" e Y8 j& n8 ]
1、 空格连接=and,如 你好 我要=%你好% and %我要%
6 V7 l; w0 q: {1 T$ H$ |7 [2、 避免内容包含字符=-,如 你好 -我要=%你好% and not like %我要% + C$ z% e; X3 u) a) `' Q
3、 |=or,如 你好|我要=%你好% or %我要% $ t8 ?/ M; O. Q% w a# t
4、 词组搜索用双引号包含,如 \i love this game\=%i love this game%,而非=i and love and this and game
5 C0 G" M% A, g* u5、 $为定界符,如 $你好=以 你好 开头的字符,你好$=以 你好 结尾的字符
5 H" f1 }+ u& j, |, U" V% s' a1 T* {* T& v
组合查询
! x4 Z; M( {% o1 Q* _5 m) S: s1 O如 \i love this game\|-你好=%i love this game% and not like %你好%
: v0 k n0 j4 D1 |1 a如 我要$|-$你好=%我要 or not like 你好% 4 A# a, k$ \' Q
如 $\i love this game\ $你好$=i love this game% and like 你好 0 P2 ?7 x- o1 k/ y2 u) o, l
6 Q7 z! Z3 a/ J2 q9 c0 h& h
调用:
$ c3 a) L3 L( q3 [/ }( z! x, h. f8 `! P8 |0 L
Dim SQL,WSQL- q+ @# y! O, j1 T/ P8 x
Dim Keyword,Field& A. y7 _3 i# s0 H6 @# M, G- C/ [
0 R! y6 o8 H9 o5 X'get keyword8 Y) k* P! A0 S4 X) ^
Keyword=Request("keyword")" Q: f3 |' E+ K1 V
Field=Request("field")( ]+ O7 O1 W2 C' O5 d
3 _: s. Z+ y- G$ k# l- ~, o' G
WSQL=MakeSQLQuery(Field,Keyword). L, \! g- W9 n8 l
: S C U7 i: `3 R1 V/ [- u. `2 ISQL="SELECT * FROM table1 WHERE 1=1 "&WSQL |
|