Tải bản đầy đủ - 0 (trang)
Hack 51. Get Cleaner Or-Based Criteria

Hack 51. Get Cleaner Or-Based Criteria

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

Figure5-46showsanalternatewaytosetuptheOrcriteria.

Thespecifiedstatesareputononerow,withOrstatements

throughout.However,thisdesignalsosuffersfrombecoming

unwieldyifmorestatesareadded.Eachadditionalstatebeing

addedalsorequiresanotherOroperator,sotheexpressioncan

becomequitelong.



Figure5-46.Alongcriteriastatement



TheInoperatoristhesolutiontothisdilemma.TheInoperator

isperfectforestablishingOr-basedcriteria.WhereasinFigure546theinclusionofeachstaterequiresanotherOroperator,only

oneInoperatorisnecessary,asshowninFigure5-47.



Figure5-47.UsingtheInoperator



UsingtheInoperatormakesiteasytoaddmorestatestothe

criteria.Justmakesureyouseparateeachstateabbreviation

withacomma.



Hack52.GetCleanerAnd-BasedCriteria



RemovetheneedformultipleAndstatementsby

combiningtheInandNotoperators.

Sometimes,criteriaaresetuptofilteroutcertainrecords

insteadofincludingthem.Thisreversaloflogicmakessensein

situationsinwhichyouwanttoreturnmostoftherecords,but

notallofthem."GetCleanerOr-BasedCriteria"[Hack#51]

showshowtousetheInoperatortobettermanageOrbased

criteria.Whenyousetupcriteriatobeexcluded,however,use

theAndoperator.Forexample,youmightask,"Givemeall

states,exceptCaliforniaandNewMexico."

Figure5-48showsaquerydesignthatexcludessixstatesfrom

thequeryresults.Asnewstatesareaddedtothislist,an

additionalAndoperatorisrequired.Eventually,thismethodof

writingmultipleAndoperatorsbecomestiresomeandyouendup

havingtoscrolltoreadthroughitall.



Figure5-48.UsingmultipleAndoperatorstofilter

outrecords



TheInoperatormightcometomindasawaytoreducethe

longcriteriastatement.However,thepointistonotincludethe

criteria.ThesolutionistouseboththeInandtheNotoperators.

Notisalogicaloperator:itreversesacondition.Includingitwith

anInoperatorresultsinalistofitemsnottoinclude,which

worksperfectlyforthistypeofquery.

Figure5-49showstheimprovedquery,inwhichthemultipleAnd

statementsareremoved.

Thequeryreturnsthesameresults,withaless-clutteredSQL

statement.



Figure5-49.UsingNotandIntogether



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

Hack 51. Get Cleaner Or-Based Criteria

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

×