group by 的执行过程,先排序,再去重。 查询mysql 中的用户,允许那些网段登录。 select user,group_concat(host) from mysql.user group by user;![]()
only_full_group_by 说明: 在mysql 5.7版本中,sql_mode参数中自带,在5.6 中没有,8.0中又取消了。 在带有group by 字句的selct 语句中,select 后面跟的列(非主键列),要么是group by 后面的列,要么需要在聚合函数中包裹。 select user,host from mysql.user group by user; # 这条语句在mysql 5.7 中会报错。 select user,group_concat(host) from mysql.user group by user; mysql 5.7 关闭only_full_group_by
如果关掉only_full_group_by 在有group by 子句的select 语句中,有重复的数据时,只会取第一个。
关于多表连接语法规则 1、首先找涉及到的所有表 2、找到表和表之间的关联列 3、关联条件写在ON后面 4、所有需要查询的信息放在select 后 5、其他的过滤条件 where group by having order by limit 往最后放 注意(和性能有关): 对多表连接中,驱动表(from后的第一张表)选择数据行少的表,后续所有表的关联列尽量是主键或者唯一键(表设计的时候),如果不能保证,至少建立一个索引。 ++++++++++++ 扩展类 内容--------元数据获取 元数据介绍: 是存储在"基表"中。 只能通过专用的DDL语句,DCL语句进行修改。比如create database abc; 通过专用视图和命令进行 元数据 的查询。 information_schema 中保存了大量 元数据 查询的视图 show 命令是封装好的功能,提供元数据查询的一些基础功能 **** ++++++++++++++++++++information_schema 的基本应用 *** information_schema 是一个虚拟库,主要是收集各个库的信息,在每次启动mysql 的时候会自动生成,不占用空间,运行在内存里面。相当于一个内存视图。 视图:(将我们经常用到的查询语句,起个别名。以后只需要查询这个别名就可以了。) create view abc as select teacher.tname,group_concat(student.sname),count(student.sname) from teacher join course on teacher.tno=course.tno join score on course.cno=score.cno join student on score.sno=student.sno where teacher.tname='oldguo' group by teacher.tno; select * from abc;
use information_schema; desc tables;
TABLE_SCHEMA 表所在的库名 TABLE_NAME 表名 ENGINE 存储引擎 TABLE_ROWS 数据行 AVG_ROW_LENGTH 平均行长度 INDEX_LENGTH 索引长度 应用实例: 查询mysql 中,所有库和表的信息 use information_schema; select table_schema,table_name from information_schema.tables; 扩展: select table_schema,group_concat(table_name) from information_schema.tables group by table_schema;
查询一下,所有innodb引擎的表 select table_schema,table_name,engine from information_schema.tables where engine='innodb'; ---- 统计 world 库的 city 表占用空间大小,以KB显示。 平均行长度 * 行数 + 索引长度 = 一张表的数据量的真实大小 AVG_ROW_LENGTH * TABLE_ROWS + INDEX_LENGTH select table_name, (avg_row_length*table_rows+index_length)/1024 from information_schema.tables where table_schema='world' and table_name='city'; ##以KB显示
统计 world 库,数据量总大小,以KB显示 select table_schema, sum((avg_row_length*table_rows+index_length))/1024 from information_schema.tables where table_schema='world';
统计mysql 中,每个库的数据量大小,以KB显示。 select table_schema, sum((avg_row_length*table_rows+index_length))/1024 from information_schema.tables;
统计每个库的数据量大小,并按数据量从大到小排序 select table_schema,sum((avg_row_length*table_rows+index_length))/1024 as data_k from information_schema.tables group by table_schema order by data_k desc;
+++++++ 配合concat()函数拼接语句或命令 例子:模仿以下语句,进行数据库的分库分表备份 mysqldump -uroot -p123 world city >/bak/world_city.sql select concat('mysqldump -uroot -p123',' ',table_schema,' ',table_name,'>/bak/table_schema_table_name.sql;') from information_schema.tables;
模仿以下语句,进行批量生成对world库下所有表进行操作 alter table world.city discard tablespace; select concat('alter table',' ',table_schema,'.',table_name,' ','discard tablespace;') from information_schema.tables where table_schema='world';
+++++ show 介绍 **** show databases; 查看数据库名 show tables; 查看表名 show create database xx; 查看 建库语句 show create table xx; 查看 建表语句 show processlist; 查看所有用户连接情况 show charset; 查看支持的字符集 show collation; 查看所有支持的校对规则 show grants from xx; 查看用户的权限信息 show variables like '%xx%'; 查看参数信息 show engines; 查看所有支持的存储引擎类型 show index from xxx; 查看表的索引信息 show engine innodb status\G 查看innodb 引擎详细状态信息 show binary logs; 查看二进制日志的列表信息 show binlog events in '二进制日志文件名'; 查看二进制日志的事件信息 show master status; 查看mysql 当前使用二进制日志信息 show slave status\G 查看从库状态信息 show relaylog events in '中继日志文件名'; 查看中继日志的事件信息 show status like ' '; 查看数据库整体状态信息

