Tải bản đầy đủ (.pdf) (9 trang)

Tài liệu Using SQL phần 2 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (23.97 KB, 9 trang )


Figure 3.6: Products where ProductName is like 'Cha%'
The next SELECT statement uses the LIKE operator to retrieve products where the
ProductName column is like '[ABC]%':
SELECT ProductID, ProductName
FROM Products
WHERE ProductName LIKE '[ABC]%';

Figure 3.7
shows the results of this SELECT statement. LIKE '[ABC]%' matches
products with a name that starts with any of the letters A, B, or C, and ends with any
number of characters.

Figure 3.7: Products where ProductName is like '[ABC]%'
The next SELECT statement uses the LIKE operator to retrieve products where the
ProductName column is like '[^ABC]%':
SELECT ProductID, ProductName
FROM Products
WHERE ProductName LIKE '[^ABC]%';
Figure 3.8
shows the results of this SELECT statement. LIKE '[^ABC]%' matches
products with names that don't start with any of the letters A, B, or C, and end with any
number of characters.

Figure 3.8: Products where ProductName is like '[^ABC]%'
The next SELECT statement uses the LIKE operator to retrieve products where the
ProductName column is like '[A-E]%':
SELECT ProductID, ProductName
FROM Products
WHERE ProductName LIKE '[A-E]%';
Figure 3.9


shows the results of this SELECT statement. LIKE '[A-E]%' matches products
with names that start with any of the letters A through E, and end with any number of
characters.

Figure 3.9: Products where ProductName is like '[A-E]%'
Specifying a List of Values
You use the IN operator in a WHERE clause to retrieve rows with columns that contain
values in a specified list. For example, the following SELECT statement uses the IN
operator to retrieve products with a ProductID of 1, 2, 5, 15, 20, 45, or 50:
SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice
FROM Products
WHERE ProductID IN (1, 2, 5, 15, 20, 45, 50);

Here's another example that displays the OrderID column from the Orders table for the
rows where the CustomerID column is in the list retrieved by a subquery; the subquery
retrieves the CustomerID column from the Customers table where the CompanyName is
like 'Fu%':
SELECT OrderID
FROM Orders
WHERE CustomerID IN (
SELECT CustomerID
FROM Customers
WHERE CompanyName LIKE 'Fu%'
);
The results of the subquery are used in the outer query.
Specifying a Range of Values
You use the BETWEEN operator in a WHERE clause to retrieve rows with columns that
contain values in a specified range. For example, the following SELECT statement uses
the BETWEEN operator to retrieve products with a ProductID between 1 and 12:
SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice

FROM Products
WHERE ProductID BETWEEN 1 AND 12;
Here's another example that displays the OrderID column for the rows from the Orders
table where the OrderDate is between '1996-07-04' and '1996-07-08':
SELECT OrderID
FROM Orders
WHERE OrderDate BETWEEN '1996-07-04' AND '1996-07-08';

Reversing the Meaning of an Operator
You use the NOT keyword with an operator in a WHERE clause to reverse the meaning
of that operator. For example, the following SELECT statement uses the NOT keyword
to reverse the meaning of the BETWEEN operator:
SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice
FROM Products
WHERE ProductID NOT BETWEEN 1 AND 12;


Note You can use the NOT keyword to reverse other operators, for example, NOT LIKE,
NOT IN.
Retrieving Rows with Columns Set to Null
Earlier, I mentioned that columns can contain null values. A null value is different from a
blank string or zero: A null value represents a value that hasn't been set, or is unknown.
You can use the IS NULL operator in a WHERE clause to determine if a column contains
a null value. For example, the following SELECT statement uses the IS NULL operator
to retrieve customers where the Fax column contains a null value:
SELECT CustomerID, CompanyName, Fax
FROM Customers
WHERE Fax IS NULL;
Figure 3.10
shows the results of this SELECT statement.


Figure 3.10: Using the IS NULL operator to retrieve customers where Fax contains a null
value
As you can see, null values are displayed as NULL in the Query Analyzer.
Specifying Multiple Conditions
You can use the logical operators shown in Table 3.3
to specify multiple conditions in a
WHERE clause.
Table 3.3: LOGICAL OPERATORS
OPERATOR DESCRIPTION
a AND b Evaluates to true when a and b are both true
a OR b Evaluates to true when either a or b are true
NOT a Evaluates to true if a is false, and false if a is true
For example, the following SELECT statement uses the AND operator to retrieve
products where the UnitsInStock column is less than 10 and the ReorderLevel column is
less than or equal to 20:
SELECT ProductID, ProductName, UnitsInStock, ReorderLevel
FROM Products
WHERE UnitsInStock < 10
AND ReorderLevel <= 20;
Figure 3.11
shows the results of this SELECT statement.

×