美文网首页
《SQL必知必会》—— 视图

《SQL必知必会》—— 视图

作者: u14e | 来源:发表于2017-11-17 16:42 被阅读6次

    视图

    -- 利用视图简化复杂的联结
    CREATE VIEW productscustomers AS
    SELECT cust_name, cust_contact, prod_id
    FROM customers, orders, orderitems
    WHERE orders.cust_id = customers.cust_id AND orders.order_num = orderitems.order_num;   -- 返回订购任意产品的所有顾客
    
    SELECT * FROM productscustomers WHERE prod_id = 'RGAN01';                               -- 返回订购了RGAN01的所有顾客
    
    -- 利用视图重新格式化检索出的数据
    CREATE VIEW VendorLocation AS
    SELECT CONCAT(vend_name,' (', vend_country, ')') AS vend_title
    FROM vendors;
    
    SELECT * FROM vendorlocation;
    
    -- 使用视图过滤不要的数据
    CREATE VIEW CustEmailList AS
    SELECT cust_id, cust_name, cust_email
    FROM customers WHERE cust_email IS NOT NULL;
    
    SELECT * FROM CustEmailList;
    
    -- 使用视图与计算字段
    CREATE VIEW OrderItemsExpanded AS
    SELECT order_num, prod_id, quantity, item_price, quantity*item_price AS expanded_price
    FROM orderitems;
    
    SELECT * FROM OrderItemsExpanded WHERE order_num = 20008;
    

    相关文章

      网友评论

          本文标题:《SQL必知必会》—— 视图

          本文链接:https://www.haomeiwen.com/subject/txbjvxtx.html