1. Trang chủ
  2. » Công Nghệ Thông Tin

The Language of SQL- P19 potx

5 234 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 91,09 KB

Nội dung

The result is: CustomerName State QuantityPurchased William Smith IL 4 Brenda Harper NY 5 Notice there are two sets of parentheses in this statement. Our use of parentheses here is analogous to the parentheses used in the composite functions seen in Chapter 4. With regard to functions, if there is more than one set of parentheses, the innermost set of functions always gets evaluated first. The same is true of parentheses used in Boolean expressions. In this example, the innermost set of parentheses contains: (QuantityPurchased >= 3 AND QuantityPurchased <= 10) After this is evaluated for each row, you can then proceed outward to the second set of parentheses: (State ¼ 'IL' AND (QuantityPurchased >= 3 AND QuantityPurchased <= 10)) Finally, you add in the final line in the WHERE clause (which is not enclosed in any parentheses at all): WHERE State ¼ 'NY' OR (State ¼ 'IL' AND (QuantityPurchased >= 3 AND QuantityPurchased <= 10)) The NOT Operator In addition to the AND and OR operators, it is often useful to use the NOT operator to express a complex logical condition. The NOT expresses a negation, or opposite, of whatever follows the NOT. Here’s a simple example: SELECT CustomerName, State, Chapter 8 ■ Boolean Logic76 QuantityPurchased FROM Orders WHERE NOT State ¼ 'NY' The result is: CustomerName State QuantityPurchased William Smith IL 4 Natalie Lopez CA 10 This specifies a selection of rows where the state is not equal to NY. In this simple case, the NOT operator is not truly necessary. The previous statement can also be accomplished via the following equivalent statement: SELECT CustomerName, State, QuantityPurchased FROM Orders WHERE State <> 'NY' In this situation, the not equals operato r (<>) accomplishes the same thing as the NOT operator. Here’s a more complex example with the NOT operator: SELECT CustomerName, State, QuantityPurchased FROM Orders WHERE NOT (State ¼ 'IL' OR State ¼ 'NY' ) The result is: CustomerName State QuantityPurchased Natalie Lopez CA 10 The NOT Operator 77 When the NOT operator is used before a set of parentheses, it negates everything in the parentheses. In this example, you are looking for all rows where the state is not Illinois or New York. Again, note that the NOT operator is not strictly necessary in this example. The previous query can also be accomplished via the following equi valent statement: SELECT CustomerName, State, QuantityPurchased FROM Orders WHERE State <> 'IL' AND State <> 'NY' You may need to think a minute about why the previous two statements are really equivalent. The first statement utilizes the NOT operator and a logical expression with an OR operator. The second statement converts the same logic into an expression with an AND operator. Here’s a final example of how the NOT operator can be used in a complex statement: SELECT CustomerName, State, QuantityPurchased FROM Orders WHERE NOT (State ¼ 'IL' AND QuantityPurchased > 3) The result is: CustomerName State QuantityPurchased Natalie Lopez CA 10 Brenda Harper NY 5 Chapter 8 ■ Boolean Logic78 As before, there is another way to express the previous statement without using the NOT: SELECT CustomerName, State, QuantityPurchased FROM Orders WHERE State <> 'IL' OR QuantityPurchased <= 3 As seen in these examples, it may not be logically necessary to use the NOT operator in complex expressions with arithmetic operators such as equals (=) and less than (<). However, it’s often more straightforward to place a NOT in front of a logical expression than to convert that expression to one that doesn’t use the NOT. In other words, the NOT operator can provide a useful way of expressing your logical thoughts. The BETWEEN Operator We will now turn to two speci al operators that can simplify expressions that would ordinarily require the OR or AND operators. These are the BETWEEN and IN operators. The BETWEEN operator allows you to abbreviate an AND expres- sion with greater than or equal to (>=) and less than or equal to (<=) operators into one simple expression with a single operator. Here’s an example. Let’s say you want to select all rows with a quantity purchased between 5 and 20. You can issue the following SELECT statement: SELECT CustomerName, QuantityPurchased FROM Orders WHERE QuantityPurchased >= 5 AND QuantityPurchased <= 20 or you can issue this equivalent statement that utilizes the BETWEEN operator: SELECT CustomerName, QuantityPurchased FROM Orders WHERE QuantityPurchased BETWEEN 5 AND 20 The BETWEEN Operator 79 In both cases, the SELECT returns this data: CustomerName QuantityPurchased Natalie Lopez 10 Brenda Harper 5 The BETWEEN operator always requires a corresponding AND placed between the two numbers. Note the relative simplicity of the BETWEEN operator. Also notice that the BETWEEN keyword is equi valent only to the greater than or equal to (>=) and less than or equal to (<=) operators. It can’t be used to express something simply greater than (>) or less than (<) a range of numbers. In this example, the row for Brenda Harper is selected since the quantity is equal to 5, and therefore is between 5 and 20. The NOT operator can be used with the BETWEEN. For example, this SELECT: SELECT CustomerName, QuantityPurchased FROM Orders WHERE QuantityPurchased NOT BETWEEN 5 AND 20 retrieves this data: CustomerName QuantityPurchased William Smith 4 The IN Operator Just as the BETWEEN represents a special case of the AND operator, the IN operator allows for a special case of the OR. Let’s say you want to see rows where the state is Illinois or New York. You can issue this SELECT statement: SELECT CustomerName, State FROM Orders WHERE State ¼ 'IL' OR State ¼ 'NY' Chapter 8 ■ Boolean Logic80 . parentheses here is analogous to the parentheses used in the composite functions seen in Chapter 4. With regard to functions, if there is more than one set of parentheses, the innermost set of functions always. QuantityPurchased William Smith 4 The IN Operator Just as the BETWEEN represents a special case of the AND operator, the IN operator allows for a special case of the OR. Let’s say you want to see rows where the state. 'NY' ) The result is: CustomerName State QuantityPurchased Natalie Lopez CA 10 The NOT Operator 77 When the NOT operator is used before a set of parentheses, it negates everything in the parentheses.

Ngày đăng: 05/07/2014, 05:20