CHAPTER6 Filtering Your Data
±¾Õ½éÉÜWHERE´Ó¾ä.
predicates
Comparison,BETWEEN,IN, LIKE, and IS NULL.
We¡¯ll cover theother two¡ªQuantified and EXISTS¡ªin Chapter 11, Subqueries.
Comparison
Equality and Inequality |
= ,<> |
Less Than and Greater Than |
<,> |
less than or equal to |
<= |
greater than or equal to |
>= |
combinecomparisons using AND and OR
Range
TheBETWEEN . . . AND predicatedefines the range by using the value of the second value expression
as the start pointand the value of the third value expression as the end point.
Both the start pointand end point are part of the range.
ϰ¹ßʹÓà (Value Expression1 <= ValueExperssion2) and (Value Expression1 >= Value Experssion3)µÄ¸÷λͬѧ,¿ÉÒÔ¿¼ÂÇʹÓÃBETWEEN . . . ANDÌæ´ú,ÕâÑùSQLÓï¾ä»á¸üÈÝÒ×ÔĶÁÀí½â.<†·Ÿ"/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHAgbGFuZz0="en-US">
Set Membership
Pattern Match
Apattern string can consist ofany logical combination of regular string characters and
two special wildcardcharacters: the percent sign (%) and the underscore (_).
Thepercent sign represents zero or more arbitrary regular characters, and the underscore represents a single arbitraryregular character.
%Óë_ͨÅä·ûÀà似ÓÚÕýÔò±í´ïʽÖеÄ*Óë?
Óöµ½Í¨Åä·ûÓëÕý³£×Ö·û»ìÏýµÄÇé¿öÔõô°ì?±ÈÈçÎÒÃÇÐèҪƥÅ京ÓÐÏ»®ÏßµÄ×Ö·û´®Ôõô°ì?ÕâÒ²Êǹ¦ÄÜ×Ö·û°üº¬ÔÚ³£¹æ×Ö·û»áÓöµ½µÄ»ìÏýÇé¿ö,±àÂëÖо³£Åöµ½.ÕâʱÎÒÃÇÐèÒª½«¹¦ÄÜ×Ö·ûתÒåΪ³£¹æ×Ö·û,ÎÒÃÇʹÓÃESCAPE¹Ø¼ü×ÖʵÏÖÕâ¸ö¹¦ÄÜ.
¾Ù¸öÀý×Ó,һĿÁËÈ»:
¡°Showme a list of products that have product codes beginning with ¡®G_00¡¯ and ending in a single number orletter.¡±
SQL SELECT ProductName, ProductCode
FROMProducts
WHEREProductCode LIKE 'G\_00_' ESCAPE '\'
Keep in mind thatthe character you use as an escape character should not be
part of the valuesyou¡¯re trying to retrieve.
Null
ÅжÏValue ExpressionÊÇ·ñΪNULLµÄʱºòÇ벻ҪʹÓÃValue Expression = NULL,ÕâÊdz£·¸µÄС´íÎó.
Excluding Rows with NOT
Order of Precedence
Whenyou treat a combined set of conditions as a single unit, by definitionit becomes a
searchcondition, and you must enclose it in parentheses.
Ìí¼ÓÀ¨ºÅÒÔ±ÜÃâ¿ÉÄܵĻìÏý.
Whenyou need to use multiple conditions, make certain that the condition thatexcludes the
most rows from theresult set is processed first so that your database can
potentially find theanswer faster.