• MySQL 高级查询操作


    MySQL 高级查询操作

    一、预告

    最近刚刚入职了一家连锁大公司,从事硬件工程师工作(好吧,其实是做一些简单修理...)。我发现了什么呢,那就是不管到哪个行业那个阶段,数据库是必须会查询的。在早期,我写过SQL 简单新建删除权限修改,但毕竟过于基础。现在我想写一些高端查询操作,具体可以参考下方目录或大小标题段落。

    二、简单查询

    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    +--------------------+
    3 rows in set (0.00 sec)
    

    看起来是不是很简单,只是显示一下数据库嘛。

    MariaDB [(none)]> use information_schema;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    MariaDB [information_schema]> show tables;
    +---------------------------------------+
    | Tables_in_information_schema          |
    +---------------------------------------+
    | CHARACTER_SETS                        |
    | CLIENT_STATISTICS                     |
    | COLLATIONS                            |
    | COLLATION_CHARACTER_SET_APPLICABILITY |
    | COLUMNS                               |
    | COLUMN_PRIVILEGES                     |
    | ENGINES                               |
    | EVENTS                                |
    | FILES                                 |
    .........................................
    

    但如果换成了显示数据表,情况就一下子变得复杂了 。

    **问题: **如果我想显示 show 指令的显示结果,该如何操作。

    三、显示筛选

    MariaDB [information_schema]> show tables like 'TABLE%';
    +---------------------------------------+
    | Tables_in_information_schema (TABLE%) |
    +---------------------------------------+
    | TABLES                                |
    | TABLESPACES                           |
    | TABLE_CONSTRAINTS                     |
    | TABLE_PRIVILEGES                      |
    | TABLE_STATISTICS                      |
    +---------------------------------------+
    5 rows in set (0.00 sec)
    

    再问: 如果我想把 数据名 单独拿出来使用,有没有可能可以操作?

    四、存储过程

    在继续往下看实验之前,得和你说下 MYSQL 的一些黑科技。

    在关系数据库内,存储过程 是一个 ANSI 标准的只读视图表。它提供的信息关于 tables, views, columns, procedures 在 information_schema 数据库内。

    所以,要解决第二个问题。

    你要有一个清晰的概念,即 SHOW 指令本身调用的数据来源即为 information_schema .

    MariaDB [information_schema]> select schema_name from information_schema.schemata;
    +--------------------+
    | schema_name        |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    4 rows in set (0.00 sec)
    
    MariaDB [information_schema]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    4 rows in set (0.00 sec)
    
    MariaDB [information_schema]> 
    

    **问题 3: ** 虽然你已经知道了 存储过程 ,并且将信息全部使用 select 指令调出。还是没有办法进行操作啊,操作怎么的得 独立 出来吧。

    提示: 如何显示数据库的命令位于附表二

    五、查询语句

    1、作为变量

    MariaDB [(none)]> set @var := '123456';
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [(none)]> select @var;
    +--------+
    | @var   |
    +--------+
    | 123456 |
    +--------+
    1 row in set (0.00 sec)
    

    提示: :== 区别在于,前者是赋值后者是比较。在有些情况下直接使用等号也是可以的,建议使用 := 这个写法。一些常用系统函数,位于 “附表一” 。

    MariaDB [(none)]> set @t1 := (select schema_name from information_schema.schemata);
    ERROR 1242 (21000): Subquery returns more than 1 row
    MariaDB [(none)]> set @t1 := (select schema_name from information_schema.schemata LIMIT 1);
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [(none)]> select @t1;
    +--------------------+
    | @t1                |
    +--------------------+
    | information_schema |
    +--------------------+
    1 row in set (0.00 sec)
    
    MariaDB [(none)]> 
    

    **提示: **虽然可以使用变量的方式,但是很明显的是一个数据一个变量。

    2、函数调用

    MariaDB [(none)]> set @t2 := (select group_concat(schema_name) from information_schema.schemata);
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [(none)]> select @t2;
    +--------------------------------------------------+
    | @t2                                              |
    +--------------------------------------------------+
    | information_schema,mysql,performance_schema,test |
    +--------------------------------------------------+
    1 row in set (0.00 sec)
    
    MariaDB [(none)]> 
    

    **函数名称: ** group_concat() 返回串联的字符串从而把列变成了行。

    3、写入数据表

    MariaDB [(none)]> create table test.test select schema_name from information_schema.schemata;
    Query OK, 4 rows affected (0.03 sec)
    Records: 4  Duplicates: 0  Warnings: 0
    
    MariaDB [(none)]> select * from test.test;
    +--------------------+
    | schema_name        |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    4 rows in set (0.00 sec)
    
    MariaDB [(none)]> 
    

    备注

    附表一

    [root@localhost ~]# mysql -uroot -ppassword -e "select @@version_compile_os"
    +----------------------+
    | @@version_compile_os |
    +----------------------+
    | Linux                |
    +----------------------+
    [root@localhost ~]# mysql -uroot -ppassword -e "select version()"
    +----------------+
    | version()      |
    +----------------+
    | 5.5.56-MariaDB |
    +----------------+
    [root@localhost ~]# mysql -uroot -ppassword -e "select user()"
    +----------------+
    | user()         |
    +----------------+
    | root@localhost |
    +----------------+
    

    附表二

    select TABLE_SCHEMA,TABLE_NAME from information_schema.COLUMNS;
    

    相关文献

    • MySQL 注入
    • DBAplus 社群
    • wikipedia

    博客提示

    本博客日均 IP 已经到一百以上,后续会进行数据筛选汇总,作为 Python 下 Django 博客核心资料。

    现在关注微信公众号:IT奋斗点滴

    再我整理完成后,即可获取到完整离线版,计算机学习资料。

  • 相关阅读:
    Subsets II
    Pow(x, n)
    基本数据结构 —— 二叉搜索树(C++实现)
    基本数据结构 —— 堆以及堆排序(C++实现)
    Symmetric Tree
    Same Tree
    C++中三种传递参数方法的效率分析
    Word Search
    Subsets
    Combinations
  • 原文地址:https://www.cnblogs.com/itxdm/p/MySQL_Advanced_Query_Operation.html
Copyright © 2020-2023  润新知