I would like to share with you todayAlgorithmI wrote it in C #. Other languages just need to change the syntax. Fuzzy search with a space disconnection keyword is similar to the search function of major websites. What I want is a process of splicing SQL statements. The Delimiter is a space disconnection keyword. In either case, the Delimiter is a space disconnection relationship that is "and", and the Delimiter is a space disconnection relationship that is "or. In fact, it is also very simple.CodeRight.
Link code of the keyword "or:
/// <Summary> /// Relationship of keyword "or" /// </Summary> /// <Returns> </returns> Public String Orkeywords (){ String SQL = " Select username from usertable where 1 = 1 " ; String Txtkeyword = Txttitle. Text. Trim (); If (Txtkeyword = "" ) {Clientscript. registerstartupscript ( Typeof (Page ), " AA " , " Alert ('enter a keyword! ') " , True ); Return ;} Else { Int Len = Txtkeyword. length; Int N = 0 ; String Strsql = " And ( " ; String Strkey = "" ; For ( Int I = 0 ; I <Len; I ++ ){ If (Txtkeyword [I]. tostring () =" " ){ If (I> 0 ){ If (Txtkeyword [I- 1 ]. Tostring ()! = " " ) {N ++ ; Strsql + = " Charindex (' " + Strkey + " ', Username)> 0 or " ; Strkey = "" ;}}} Else {Strkey + = Txtkeyword [I]. tostring ();}} If (N =0 ) {SQL + = " And charindex (' " + Txtkeyword + " ', Username)> 0 order by ID DESC " ;} Else {Strsql + = " Charindex (' " + Strkey +" ', Username)> 0) " ; SQL + = "" + Strsql + " Order by ID DESC " ;} // Splicing completed Return SQL ;}}
Link code with the keyword "and:
/// <Summary> /// Relationship of the keyword "and" /// </Summary> /// <Returns> </returns> Public String Andkeywords (){ String SQL = " Select username from usertable where 1 = 1 " ; String Txtkeyword = Txttitle. Text. Trim (); If (Ttxtkeyword = "" ) {Clientscript. registerstartupscript ( Typeof (Page ), " AA " , " Alert ('enter a keyword! ') " , True ); Return ;} Else { Int Len = Txtkeyword. length; Int N = 0 ; String Strsql = " And " ; String Strkey ="" ; For ( Int I = 0 ; I <Len; I ++ ){ If (Txtkeyword [I]. tostring () = " " ){ If (I> 0 ){ If (Txtkeyword [I- 1 ]. Tostring ()! = " " ) {N ++ ; Strsql + = " Charindex (' " + Strkey + " ', Username)> 0 and " ; Strkey = "" ;}}} Else {Strkey + = Txtkeyword [I]. tostring ();}} If (N = 0 ) {SQL + = " And charindex (' " + Txtkeyword + " ', Username)> 0 order by ID DESC " ;} Else {Strsql + = " Charindex (' " + Strkey + " ', Username)> 0 " ; SQL + = "" + Strsql + " Order by ID DESC " ;} // Splicing completed Return SQL ;}}
The code is very simple. I will not explain it. I will record it myself and hope it will help you.
PS: Tandy tang I wish you a pleasant time writing your code!