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

The Language of SQL- P18 pptx

5 243 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 115,48 KB

Nội dung

chapter 8 Boolean Logic Keywords Introduced: AND, OR, NOT, BETWEEN, IN, IS, NULL In the previous chapter, we introduced the concept of selection criteria, but only in its simplest form. We’re now going to expand on that concept to greatly enhance our ability to specify the rows that are returned from a SELECT.Thisiswhere the pure logic of SQL comes into play. In this chapter, we are going to introduce a number of operators that will allow you to create complex logical expressions. With these new capabilities, if a user comes to you and says that she wants a list of all female customers who live in ZIP codes 60601 through 62999, but excluding anyone who’s under the age of 30 or who doesn’t have an email address, that will be something you can provide. Complex Logical Conditions The WHERE clause introduced in the previous chapter utilized only simple selection crit eria. You saw WHERE clauses such as: WHERE QuantityPurchased ¼ 5 The condition expressed in this WHERE clause is quite simple: It returns all rows where the Quan tityPurchased column has a value of 5. In the real world, the selection of data is often far from straightforward. Accordingly, let’s now turn our attention to ways of specifying some more complex logical conditions in your selection criteria. 71 The ability to devise complex l ogical conditions is some time s called Boolean logic. This term, taken from mathematics, refe rs to the ability to formulate complex conditions that are evaluated as e ither true or false. In the aforementioned ex ample, the condition QuantityPurchased ¼ 5 is evaluated as either true or false for each row in the table . Obviously, you only want to see rows where the condition is evaluated as true. The principle keywords used to create complex Boolean logic are AND, OR,and NOT. These three operators are used to add additional functionality to the WHERE clause. In the proper combination, the AND, OR,andNOT operators, along with parentheses, can specify just about any logical expression you can imagine. The AND Operator The following examples will all be taken from this Orders table: OrderID CustomerName State QuantityPur chased PricePerItem 1 William Smith IL 4 2.50 2 Natalie Lopez CA 10 1.25 3 Brenda Harper NY 5 4.00 Here’s an example of a WHERE clause that uses the AND operator: SELECT CustomerName, QuantityPurchased FROM Orders WHERE QuantityPurchased > 3 AND QuantityPurchased < 7 The AND clause means that all conditions must evaluate to true for the row to be selected. This SELECT specifies that the only rows to be retrieved are those where the QuantityPurchased is both greater than 3 and less than 7. Therefore, only these two rows are returned: CustomerName QuantityPurchased William Smith 4 Brenda Harper 5 Chapter 8 ■ Boolean Logic72 Notice that the row for Natalie Lopez is not returned. Why? Natalie purchased a quantity of 10, which, in fact, does satisfy the first condition (QuantityPurchased > 3). However, the second condition (QuantityPurchased < 7) is not satisfied and therefore is not true. When using the AND operator, all conditions specified must be true. The OR Operator Let’s now look at the OR operator. The AND clause meant that all conditions must evaluate to true for the row to be selected. The OR clause means that the row will be selected if any of the conditions is determined to be true. Here’s an example, taken from the same table: SELECT CustomerName, QuantityPurchased, PricePerItem FROM Orders WHERE QuantityPurchased > 8 OR PricePerItem > 3 The SELECT returns this data: CustomerName QuantityPurchased PricePerItem Natalie Lopez 10 1.25 Brenda Harper 5 4.00 Why are the rows for Natalie Lopez and Brenda Harper displayed, and not the row for William Smith? The row for Natalie Lopez is selected because it meets the requirements of the first condition (QuantityPurchased > 8). It doesn’t matter that the second condition (PricePerItem > 3) isn’t true, because only one con- dition needs to be true for an OR condition. Likewise, the row for Brenda Harper is selected because the second condition (PricePerItem > 3) is true for that row. The row for William Smith isn’t selected because it doesn’t satisfy either of the two conditions. Using Parentheses Let’s say that you are only interested in orders from customers from either the state of Illinois or the state of California. Additiona lly, you only want to see Using Parentheses 73 orders where the quantity purchased is greater than 8. To satisfy this request, you put together this SELECT statement: SELECT CustomerName, State, QuantityPurchased FROM Orders WHERE State ¼ 'IL' OR State ¼ 'CA' AND QuantityPurchased > 8 When you execute this, you are expecting to get back only one row, for Natalie Lopez. This is because you have two rows for customers in Illinois or California (Smith and Lopez). But only one of those (Lopez) has a quantity purchased greater than 8. However, when you execute this statement, you see: CustomerName State QuantityPurchased William Smith IL 4 Natalie Lopez CA 10 What went wrong? Why did you get back two rows instead of one? The answer lies with how SQL interprets the WHERE clause, which contains both AND and OR operators. Like other computer languages, SQL has a predet ermined order of evaluation, which specifies the order in which various operators are interpreted. Unless told otherwise, SQL always processes the AND operator before the OR operator. So, in the previous statement, it first looks at the AND and evaluates the condition: State ¼ 'CA' AND QuantityPurchased > 8 The row that satisfies that condition is for Natalie Lopez. It then evaluates the OR operator, which allows for rows where the State equals IL. That adds in the row for William Smith. Therefore, it determines that both the William Smith and the Natalie Lopez rows meet the condition. Obviously, this isn’t what was meant. This type of problem often comes up when AND and OR operators are combined in a single WHERE clause. The way to resolve Chapter 8 ■ Boolean Logic74 the ambiguity is to use parentheses to specify the exact order of evaluation that you would like. Anything in parentheses is always evaluated first. Here’s how parentheses can be added to the previous SELECT to correct the situation: SELECT CustomerName, State, QuantityPurchased FROM Orders WHERE (State ¼ 'IL' OR State ¼ 'CA') AND QuantityPurchased > 8 When this is executed, you now see this data: CustomerName State QuantityPurchased Natalie Lopez CA 10 The parentheses in the SELECT statement force the OR expression (State ¼ ‘IL’ OR State ¼ ‘CA’) to be evaluated first. This produces the intended result. Multiple Sets of Parentheses Let’s say that you want to select two different sets of rows from the Orders table: first, rows for customers in New York, and second, rows for customers in Illinois who have made a purchase with a quantity between 3 and 10. The following SELECT accomplishes this requirement: SELECT CustomerName, State, QuantityPurchased FROM Orders WHERE State ¼ 'NY' OR (State ¼ 'IL' AND (QuantityPurchased >= 3 AND QuantityPurchased <= 10)) Multiple Sets of Parentheses 75 . rows where the Quan tityPurchased column has a value of 5. In the real world, the selection of data is often far from straightforward. Accordingly, let’s now turn our attention to ways of specifying. Parentheses Let’s say that you are only interested in orders from customers from either the state of Illinois or the state of California. Additiona lly, you only want to see Using Parentheses. in the row for William Smith. Therefore, it determines that both the William Smith and the Natalie Lopez rows meet the condition. Obviously, this isn’t what was meant. This type of problem often

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

TỪ KHÓA LIÊN QUAN