Tải bản đầy đủ - 0 (trang)
Hack 53. Create an Outer Join

Hack 53. Create an Outer Join

Tải bản đầy đủ - 0trang

Figure5-50.Returnedrecordsfromaleftjoin

query



Figure5-51.Settingjoinproperties



Asyoucansee,thereisnooptiontocreateanouterjoin,which

wouldreturnalltherecordsthatmatch,plustherecordsthat

don'tmatchfrombothtables.Thetricktodoingthisisto

simplyassemblethethreetypesofavailablejoinqueriesinto

onequery.ThisfinalqueryusestheUnionoperatortoassemble

theresultsfromtheotherthreequeries.

AUnionqueryworksonlywithstraightSQLstatements.Youcan

entertheSQLdirectlyintoanewqueryor,tomakeiteasier,

copythegeneratedSQLfromthethreejointypesandpasteit

intoanewquery.Allyouneedtodoisstartthesecondand

thirdmini-SQLSelectstatementswiththeUnionoperatorina

newquery,likethis:

SELECTtblCustomer2.LastName,tblSales2.PurchaseDate

FROMtblCustomer2INNERJOINtblSales2ON

tblCustomer2.CustomerID=tblSales2.Customer_ID

Union

SELECTtblCustomer2.LastName,tblSales2.PurchaseDate



FROMtblCustomer2LEFTJOINtblSales2On

tblCustomer2.CustomerID=tblSales2.Customer_ID

Union

SELECTtblCustomer2.LastName,tblSales2.PurchaseDate

FROMtblCustomer2RIGHTJOINtblSales2ON

tblCustomer2.CustomerID=tblSales2.Customer_ID;



Figure5-52showstheresultofrunningtheSQL.



Figure5-52.Theresultsofanouterjoin



Bothcolumns(eachcomesfromadifferenttable)have

blankswheretherewasnomatchingrecordintheother



tableandtherecordsthatmatcharethereaswell.



Hack54.UseRegularExpressionsinAccess

Queries



Sometimeswildcardsaren'tenough.Withalittle

hacking,youcanuseregularexpressionsinyourqueries.

AlthoughAccessallowsforsomepowerfulstringmatching(see

"WildcardcharactersandtheLikeoperator"intheAccessHelp

system),sometimesyourequireanevenmorepowerful

solution.Microsoftaddedtheabilitytouseregularexpressions

backinVersion5.0ofitsWindowsScriptingEngine,bringingit

uptoparwithJavaScript.Youcanusethispowerinsidean

Accessqueryaswell.

Althoughtheadvanceddetailsofregularexpressionsare

beyondthescopeofthishack,thisexamplewillgetyoustarted

ifyouarenewtothesubject.Ifyouneedmoreinformation,I

recommendthebookMasteringRegularExpressions(O'Reilly).

Inmanycasesit'spossibletoworkaroundthelackofbuilt-in

regularexpressionsusingAccess'swildcardcharactersand

multiplecallstodifferentstringfunctions,suchasLeft,Mid,

Right,Len,andsoon.However,onceyouseewhatyoucando

withasinglecustomfunctioncall,youcanimaginethe

advancedpossibilitiesandtimesavings.



5.16.1.CreatingtheCustomFunction

Thefirstthingweneedtodoiscreateafunctionthatcanbe

calledfromourAccessquerythattiesintotheMicrosoft

ScriptingRuntimelibrary.



Thishackassumesthemachineyouarerunninghasthelatestversion

ofMicrosoft'sScriptingEngineinstalled.Ifyouareunsure,visit

http://www.microsoft.com/scripting.



ThefollowingcodeusestheCreateObjectfunctionsothatyou

don'thavetochecktheReferenceeachtimethecodeisplacedin

anewdatabase:



PublicFunctionRegExp(strStringAsString,_

strRegExpAsString,OptionalbolIgnoreCaseAsBoolean

Boolean

DimreAsObject

Setre=CreateObject("vbscript.RegExp")

re.Pattern=strRegExp

re.IgnoreCase=bolIgnoreCase

Ifre.Test(strString)Then

RegExp=True

Else

RegExp=False

EndIf

EndFunction



Thefunctionhastworequiredparameters:thestringbeing

matchedagainstandthestringthatcontainstheregular

expression.Thethird,optionalparametertellsthefunction

whethertomatchtheregularexpressionwhileignoringthe

case;thedefaultwon'tignorethecase.



5.16.2.CreatinganExampleQuery



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 53. Create an Outer Join

Tải bản đầy đủ ngay(0 tr)

×