• mysql 视图


     创建视图

    mysql> select * from test;
    +----+------------+-------+-----------+
    | id | name       | score | subject   |
    +----+------------+-------+-----------+
    |  1 | xiaoming   |    89 | shuxue    |
    |  2 | xiaohong   |    89 | shuxue    |
    |  3 | xiaohong   |    80 | english   |
    |  4 | xiaohong   |    80 | physics   |
    |  5 | xiaohong   |    80 | astronaut |
    |  6 | xiaoming   |    80 | physics   |
    |  7 | xiaoming   |    80 | astronaut |
    |  8 | xiaoming   |    80 | english   |
    |  9 | xiaobai    |    80 | astronaut |
    | 10 | 1.2xiaobai |    80 | english   |
    | 11 | 2.2xiaobai |    80 | physics   |
    | 12 | 3xiaobai   |    80 | shuxue    |
    | 13 | 123xiaohei |    80 | astronaut |
    | 14 | xiaohei    |    80 | shuxue    |
    | 15 | xiaohei    |    80 | physics   |
    | 16 | .12xiaohei |    80 | english   |
    +----+------------+-------+-----------+
    16 rows in set (0.00 sec)
    
    
    
    mysql> select * from user;
    +------+----------+-----------+
    | id   | name     | address   |
    +------+----------+-----------+
    |    2 | xiaobai  | shandong  |
    |    3 | xiaohong | suzhou    |
    |    4 | xiaohei  | changchun |
    |    1 | xiaoming | beijing   |
    +------+----------+-----------+
    4 rows in set (0.00 sec)
    
    
    
    mysql> select test.name,score,subject,address from test,user where user.name=test.name;
    +----------+-------+-----------+-----------+
    | name     | score | subject   | address   |
    +----------+-------+-----------+-----------+
    | xiaoming |    89 | shuxue    | beijing   |
    | xiaohong |    89 | shuxue    | suzhou    |
    | xiaohong |    80 | english   | suzhou    |
    | xiaohong |    80 | physics   | suzhou    |
    | xiaohong |    80 | astronaut | suzhou    |
    | xiaoming |    80 | physics   | beijing   |
    | xiaoming |    80 | astronaut | beijing   |
    | xiaoming |    80 | english   | beijing   |
    | xiaobai  |    80 | astronaut | shandong  |
    | xiaohei  |    80 | shuxue    | changchun |
    | xiaohei  |    80 | physics   | changchun |
    +----------+-------+-----------+-----------+
    11 rows in set (0.00 sec)
    
    
    
    mysql> create view testview as (select test.name,score,subject,address from test,user where user.name=test.name);
    Query OK, 0 rows affected (0.01 sec)
    
    
    
    mysql> select * from testview;
    +----------+-------+-----------+-----------+
    | name     | score | subject   | address   |
    +----------+-------+-----------+-----------+
    | xiaoming |    89 | shuxue    | beijing   |
    | xiaohong |    89 | shuxue    | suzhou    |
    | xiaohong |    80 | english   | suzhou    |
    | xiaohong |    80 | physics   | suzhou    |
    | xiaohong |    80 | astronaut | suzhou    |
    | xiaoming |    80 | physics   | beijing   |
    | xiaoming |    80 | astronaut | beijing   |
    | xiaoming |    80 | english   | beijing   |
    | xiaobai  |    80 | astronaut | shandong  |
    | xiaohei  |    80 | shuxue    | changchun |
    | xiaohei  |    80 | physics   | changchun |
    +----------+-------+-----------+-----------+
    11 rows in set (0.00 sec)

     查看视图

    mysql> show create view testview;
    +----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
    | View     | Create View                                                                                                                                                                                                                                                                  | character_set_client | collation_connection |
    +----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
    | testview | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `testview` AS (select `test`.`name` AS `name`,`test`.`score` AS `score`,`test`.`subject` AS `subject`,`user`.`address` AS `address` from (`test` join `user`) where (`user`.`name` = `test`.`name`)) | utf8                 | utf8_general_ci      |
    +----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
    1 row in set (0.00 sec)

    删除视图

    mysql> drop view testview;
    Query OK, 0 rows affected (0.00 sec)

     参考:

    https://www.2cto.com/database/201803/726747.html

  • 相关阅读:
    NET 6开发TodoList
    TensorFlow.NET机器学习
    存储资源QPS估算
    动态创建表达式C# Expression
    DNN Platform网站迁移纪实:从Web Form 到 Asp.Net Core (Abp vNext 自定义开发)
    Serilog 来记录日志
    上周热点回顾(1.101.16)
    keycloak~自定义directgrant直接认证
    springboot~容器化环境获取真实IP地址
    Lind.DDD.Repositories.EF层介绍~实现DbContext的动态注入
  • 原文地址:https://www.cnblogs.com/sea-stream/p/11314779.html
Copyright © 2020-2023  润新知