Database Reference
In-Depth Information
Note
WHERE
Clauses and
WHERE
Clauses If a
WHERE
clause is used when retrieving data
from the view, the two sets of clauses (the one in the view and the one passed to it) are
combined automatically.
Views are exceptionally useful for simplifying the use of calculated fields. The
following is a
SELECT
statement introduced in Chapter 10. It retrieves the
order items for a specific order, calculating the expanded price for each item:
▼
Input
SELECT prod_id,
quantity,
item_price,
quantity*item_price AS expanded_price
FROM orderitems
WHERE order_num = 20005;
▼
Output
+---------+----------+------------+----------------+
| prod_id | quantity | item_price | expanded_price |
+---------+----------+------------+----------------+
| ANV01 | 10 | 5.99 | 59.90 |
| ANV02 | 3 | 9.99 | 29.97 |
| TNT2 | 5 | 10.00 | 50.00 |
| FB | 1 | 10.00 | 10.00 |
+---------+----------+------------+----------------+
To turn this into a view, do the following:
▼
Input
CREATE VIEW orderitemsexpanded AS
SELECT order_num,
prod_id,
quantity,
item_price,
quantity*item_price AS expanded_price
FROM orderitems;
To retrieve the details for order
20005
(the previous output), do the following:
▼
Input
SELECT *
FROM orderitemsexpanded
WHERE order_num = 20005;