forked from Jeanhwea/mysql-crash-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
22-using-views.sql
43 lines (35 loc) · 1.03 KB
/
22-using-views.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
-- 第22章 使用视图
-- forehead
SELECT cust_name, cust_contact
FROM customers, orders, orderitems
WHERE customers.cust_id = orders.cust_id
AND orders.order_num = orderitems.order_num
AND prod_id = 'TNT2';
-- create view
CREATE VIEW prodcust AS
SELECT cust_name, cust_contact, prod_id
FROM customers, orders, orderitems
WHERE customers.cust_id = orders.cust_id
AND orders.order_num = orderitems.order_num;
SELECT cust_name, cust_contact
FROM prodcust
WHERE prod_id = 'TNT2';
-- formatting field using views
CREATE VIEW vendorlocations AS
SELECT Concat(RTrim(vend_name), ' (', Trim(vend_country) ,')') AS vend_title
FROM vendors
ORDER BY vend_name;
SELECT * FROM vendorlocations;
-- filtering data
CREATE VIEW custemaillist AS
SELECT cust_id, cust_email
FROM customers
WHERE cust_email IS NOT NULL;
-- calculate field
CREATE VIEW orderitemsexpanded AS
SELECT order_num
prod_id,
quantity,
item_price,
quantity*item_price AS expanded_price
FROM orderitems;