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

The Language of SQL- P16 docx

5 237 0

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

THÔNG TIN TÀI LIỆU

This next example will be taken from this data: ProductID Fruit Vegetable Spice ProductDescription 1 X Apple 2 X Orange 3 X Mustard 4 X Carrot In this situation, the database contains multiple columns to indicate whether the product is a fruit, vegetable, or spice. A CASE expression to create the same output for this data is: SELECT CASE WHEN Fruit ¼ 'X' THEN 'Fruit' WHEN Vegetable ¼ 'X' THEN 'Vegetable' ELSE 'Other' END AS 'Category', ProductDescription AS 'Description' FROM Products Once again, the result is: Category Description Fruit Apple Fruit Orange Other Mustard Vegetable Carrot Since the data now uses three separate columns to indicate if the product is a fruit, vegetable, or spice, you need to use the searched format of the CASE clause in order to apply the needed logic. The simple format only works with an analysis of a single column. Due to the inherent complexity of IF-THEN-ELSE logic, the CASE expression is one ofthemorechallengingtopicsinthisbook.Inthischapter,wehavefocusedon using CASE expressions in the SELECT co lumn list. However, CASE expressions can also be utilized in other SQL clauses, such as the ORDER BY cl ause, and other clauses not yet discussed, such as the WHERE and HAVING clauses. The Searched Format 61 Let’s give just one example of additional uses of the CASE expression. Although we have not yet talked about the WHERE clause, let’s imagine that we know something about it. As will be explained in Chapter 7, the WHERE clause allows you to apply selection criteria to the rows that will be presented to the user. A typical expression might be something like: WHERE ProductDescription ¼ 'White Glove' This is a very specific directive. You only want to see rows where the product is a white glove. The value of the CASE expression is that it allows you to apply conditional logic to the value you’re looking for, perhaps based on the value of some other column. For example, you may have another column, named ProductType, which gives more information about products. Using a CASE ex- pression, you can select products that are white gloves if the ProductType equals X, or products that are socks if the ProductType equals Y. In essence, you can substitute a CASE expression for the value ‘White Glove’ in the WHERE clause in order to describe some more complex logic. Looking Ahead CASE expressions can be utilized to provide a logical evaluation for a column or expression in a SELECT columnlist. There are two basic formats for the expres- sion: the simple and the searched. A typical use is to provide translations for data items with cryptic values. Finally, although this chapter is titled ‘‘Column-Based Logic,’’ CASE expressions can be used in places other than the columns in a SELECT columnlist. They can be used anyplace where you would like to specify conditional logic for a specific column or data element. In our next chapter, we are going to move beyond logic as it applies to column values and talk about how to apply logic to the selection of entire rows. This is the topic for which you’ve been patiently waiting, no doubt. The ability to specify selection criteria in your SELECT statem ents is critical to most normal queries. In the real world, it would be very unusual to issue a SELECT statement without some sort of selection criteria. The topics discussed in the next chapter will allow you to accomplish that objective. Chapter 6 ■ Column-Based Logic62 chapter 7 Row-Based Logic Keywords Introduced: WHERE, TOP/LIMIT/ROWNUM At long last, I am now going to show you how to apply selection criteria to your tables. Up until this point, our SELECT statements have always brought back every row in the table. This would rarely be the case in real-world situations. Normally, you are interested in only retrieving data that meets certain criteria. The topics in this chapter will address this issue. If you’re selecting customers, then you would typically only get to see a subset of all your customers. If you’re retrieving orders from your customers, you prob- ably only want to see orders that meet certain conditions. If you’re looking at products, then you’re probably only interested in viewing certain types of pro- ducts. Rarely does someone want to simply see everything. Your interest (or anyone else’s) in your data is typically directed toward a small subset of dat a in order to analyze or view one particular aspect. Applying Selection Criteria Selection criteria in SQL begins with the WHERE clause. The WHERE keyword accomplishes the task of selecting a subset of rows. The logic utilized for the WHERE keyword builds on the column-based logic seen in the last chapter. The difference is that, whereas the CASE expression only allowed you to apply logic to a specific column, you are now going to apply logic to all the rows in a table. 63 This is the general format of the SELECT statement, including the WHERE clause and other clauses previously discussed: SELECT columnlist FROM tablelist WHERE condition ORDER BY columnlist As can be seen, the WHERE clause must always be between the FROM and ORDER BY clauses. In fact, if an y clause is used, it must appear in the order shown. Let’s look at an example, taken from data in this Orders table: OrderID FirstName LastName QuantityPurchased PricePerItem 1 William Smith 4 2.50 2 Natalie Lopez 10 1.25 3 Brenda Harper 5 4.00 We’ll start with a statement with a simple WHERE clause: SELECT FirstName, LastName, QuantityPurchased FROM Orders WHERE LastName ¼ 'Harper' The output is: FirstName LastName QuantityPurchased Brenda Harper 5 Since the WHERE clause stipulates to only select rows with a LastName equal to ‘Harper’, only one of the three rows in the table is returned. Notice that the desired value of the LastName column was enclosed in quotes, due to the fact that LastName is a text column. For numeric fields, no quotes are necessary. For example, the following SELECT is equally valid and would have returned the same data: Chapter 7 ■ Row-Based Logic64 SELECT FirstName, LastName, QuantityPurchased FROM Orders WHERE QuantityPurchased ¼ 5 WHERE Clause Operators In the previous statements, an equals sign (=) is used as the operator in the WHERE clause. The equals sign indicates a test for equality. The general for- mat shown above indicates that a condition follows the WHERE keyword. This condition consists of an operator with two expressions on either side. The following is a list of the basic operators that can be used in the WHERE clause: WHERE Operator Meaning = equals <> does not equal > is greater than < is less than >= is greater than or equal to <= is less than or equal to More advanced operators will be covered in the next chapter. The meaning of the equals (=) and does not equal (<>) operators should be obvious. Here’s an example of a WHERE clause with an “is greater than” operator, taken from the same Orders table: SELECT FirstName, LastName, QuantityPurchased FROM Orders WHERE QuantityPurchased > 6 The result is: FirstName LastName QuantityPurchased Natalie Lopez 10 WHERE Clause Operators 65 . to use the searched format of the CASE clause in order to apply the needed logic. The simple format only works with an analysis of a single column. Due to the inherent complexity of IF-THEN-ELSE. where the product is a white glove. The value of the CASE expression is that it allows you to apply conditional logic to the value you’re looking for, perhaps based on the value of some other. task of selecting a subset of rows. The logic utilized for the WHERE keyword builds on the column-based logic seen in the last chapter. The difference is that, whereas the CASE expression only

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

Xem thêm: The Language of SQL- P16 docx

TỪ KHÓA LIÊN QUAN

Mục lục

    Chapter 1 Relational Databases and SQL

    Microsoft SQL Server, Oracle, and MySQL

    Primary and Foreign Keys

    The Significance of SQL

    Chapter 2 Basic Data Retrieval

    Column Names with Embedded Spaces

    Chapter 3 Calculations and Aliases

    The Function of Functions

    Sorting in Ascending Order

    Sorting in Descending Order

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN