码迷,mamicode.com
首页 > 数据库 > 详细

MySQL.VIEWS

时间:2015-06-05 17:15:33      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

CREATE VIEW ProductCustomers AS
    -> SELECT cust_name, cust_contact, prod_id
    -> FROM Customers, Orders, OrderItems
    -> WHERE Customers.cust_id = Orders.cust_id
    -> AND OrderItems.order_num = Orders.order_num;

SELECT cust_name, cust_contact FROM ProductCustomers WHERE prod_id = RGAN01;
+---------------+--------------------+
| cust_name     | cust_contact       |
+---------------+--------------------+
| Fun4All       | Denise L. Stephens |
| The Toy Store | Kim Howard         |
+---------------+--------------------+


SELECT CONCAT(vend_name,  (, vend_country, ))
    -> FROM Vendors ORDER BY vend_name;
+--------------------------------------------+
| CONCAT(vend_name,  (, vend_country, )) |
+--------------------------------------------+
| Bear Emporium (USA)                        |
| Bears R Us (USA)                           |
| Doll House Inc. (USA)                      |
| Fun and Games (England)                    |
| Furball Inc. (USA)                         |
| Jouets et ours (France)                    |
+--------------------------------------------+

CREATE VIEW VendorLocations AS
    -> SELECT CONCAT(vend_name,  (, vend_country, ))
    -> AS vend_title
    -> FROM Vendors;
Query OK, 0 rows affected (0.02 sec)


CREATE VIEW CustomersEmailList AS
    -> SELECT cust_id, cust_name, cust_email
    -> FROM Customers
    -> WHERE cust_email IS NOT NULL;
Query OK, 0 rows affected (0.02 sec)


CREATE VIEW OrderItemsExpandedPrice AS
    -> SELECT order_num, prod_id, quantity, item_price, quantity * item_price AS expanded_price
    -> FROM OrderItems;
Query OK, 0 rows affected (0.01 sec)

SELECT * FROM OrderItemsExpandedPrice;
+-----------+---------+----------+------------+----------------+
| order_num | prod_id | quantity | item_price | expanded_price |
+-----------+---------+----------+------------+----------------+
|     20005 | BR01    |      100 |       5.49 |         549.00 |
|     20005 | BR03    |      100 |      10.99 |        1099.00 |
|     20006 | BR01    |       20 |       5.99 |         119.80 |
|     20006 | BR02    |       10 |       8.99 |          89.90 |
|     20006 | BR03    |       10 |      11.99 |         119.90 |
|     20007 | BR03    |       50 |      11.49 |         574.50 |
|     20007 | BNBG01  |      100 |       2.99 |         299.00 |
|     20007 | BNBG02  |      100 |       2.99 |         299.00 |
|     20007 | BNBG03  |      100 |       2.99 |         299.00 |
|     20007 | RGAN01  |       50 |       4.49 |         224.50 |
|     20008 | RGAN01  |        5 |       4.99 |          24.95 |
|     20008 | BR03    |        5 |      11.99 |          59.95 |
|     20008 | BNBG01  |       10 |       3.49 |          34.90 |
|     20008 | BNBG02  |       10 |       3.49 |          34.90 |
|     20008 | BNBG03  |       10 |       3.49 |          34.90 |
|     20009 | BNBG01  |      250 |       2.49 |         622.50 |
|     20009 | BNBG02  |      250 |       2.49 |         622.50 |
|     20009 | BNBG03  |      250 |       2.49 |         622.50 |
+-----------+---------+----------+------------+----------------+

 

MySQL.VIEWS

标签:

原文地址:http://www.cnblogs.com/fatoland/p/4554851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!