# SELECT
## 单独使用
-- select @@ xxx 查看系统参数
select @@port;
select @@basedir;
select @@datadir
select @@socket
select @@server_id;
select @@log_error
-- select 函数()
select now();
select database();
select user();
select concat('hello world')
select concat(user,'@',host) FROM mysql.user;
select group_concat(user, '@', host) from mysql.user
一:## 单表查询-from
select 列名 from syu
select * from stu(不要对大表使用)
二:## 单表查询-WHERE
select col1, col2 FROM table where colN 条件
-- where 配合等值查询
select * FROM city where countrycode='CHN';
-- where 配合比较操作符 (> < >= <= <>)
select * FROM city where population <100;
-- where 配合逻辑运算符( and or)
select * FROM city where countrycode='chn' and population>5000000;
-- where 配合模糊查询
select * from city where District like 'guang%'
-- 注意 % 不能放在前面, 因为不走索引
-- where 配合in查询
select * FROM city where CountryCode in ('chn', 'usa');
-- where 配合between and (在....之间)
select * from city where population between 1000000 and 2000000;
三:单表查询-GROUP BY + 聚合函数公式
聚合函数:
max():最大值
min()最小值
avg():平均值
sum():总和
count():个数
group_concat() :列转行
1: 统计中国每个省的总人口数:
select district, sum(population) FROM city where countrycode='chn' GROUP BY(district);
2: 统计世界上每个国家的城市数量:
select countrycode,count(countrycode) FROM city GROUP BY(countrycode);
3: 统计中国,每个省的城市名字列表
select district, GROUP_CONCAT(name) FROM city where countrycode='chn' GROUP BY(district);
四:## 单表查询-HAVING
统计中国每个省的总人口数, 只打印总人口数小于500w的信息
select district, sum(population) FROM city where countrycode='chn' GROUP BY(district) HAVING SUM(population)>5000000;
注意:having条件师不走索引的,一般可以用临时表解决
五:## 单表查询-order BY
统计中国每个省的总人口, 找出大于500w的,并按总人口从大到小排序
select district, sum(population) FROM city where countrycode='chn' GROUP BY(district) HAVING SUM(population)>5000000 ORDER BY SUM(population) desc;
六:## 单表查询-limit
统计中国每个省的总人口, 找出大于500w的,并按总人口从大到小排序,只显示前3名
select district, sum(population) FROM city where countrycode='chn' GROUP BY(district) HAVING SUM(population)>5000000 ORDER BY SUM(population) desc limit 3;
limit N,M 跳过N行,显示M行,
limit M, offset N: 到M行, 跳过N行
distinct: 去重
select distinct(countrycode) from city;
七:## 联合查询-union all
select * FROM city where CountryCode in ('chn', 'usa');
等于:
select * from city where CountryCode='chn'
union ALL
select * from city where CountryCode='usa'
说明: 一般情况下,我们会将in 或者 or语句改写成union all, 来提高性能
union 去重复
union all 不去重复
八:## 多表连接-
join on
a join b on a.id= b,id
1: 查询你zhang3,学习的课程名称有哪些:
use school
select student.sname, course.cname FROM student join sc on student.sno=sc.sno join course on sc.cno=course.cno where student.sname='zhang3';
九: information_schema 视图库(虚拟库) 保持一些经常用到的查询语句
use information_schema ;
create view zs_v as select student.sname, course.cname FROM student join sc on student.sno=sc.sno join course on sc.cno=course.cno where student.sname='zhang3';
select * from zs_v;
表: 元数据+数据行
元数据存储在‘基表’, 是我们没用权限访问的
mysql 给我们提供了DDL, DCL来进行对元数据的修改
提供了 information_schema和show的语句查询元数据
desc information_schema.tables;
table_schema 库名
table_name 表名
engine 引擎
table_rows 表的行数
avg_row_length 表中行的平均行(字节)
index_length 索引的占用空间大小(字节)
1:查询整个数据中所有库和多对应表的信息
use information_schema;
select table_schema, GROUP_CONCAT(table_name) FROM information_schema.tables GROUP BY table_schema;
2: 统计world数据库下每张表的磁盘空间占用
select table_schema, table_name, (table_rows*table_rows+index_length)/1024 as size_KB FROM information_schema.tables where table_schema='world';
3:生成整个数据库下的所有表的单独备份语句
mysqldump-uroot-p123 world city>/tmp/world_city.sqlSELECTCONCAT("mysqldump -uroot -p123 ",table_schema," ",table_name," >/tmp/",table_schema,"_",table_name,".sql")FROMinformation_schema.tablesWHEREtable_schemaNOTIN('information_schema','performance_schema','sys')INTOOUTFILE'/tmp/bak.sh';
CONCAT("mysqldump -uroot -p123 ",table_schema," ",table_name," >/tmp/",table_schema,"_",table_name,".sql")
4. show 命令
show databases; #查看所有数据库
show tables; #查看当前库的所有表
SHOW TABLES FROM #查看某个指定库下的表
show create database world #查看建库语句
show create table world.city #查看建表语句
show grants for root@'localhost' #查看用户的权限信息
show charset; #查看字符集
show collation #查看校对规则
show processlist; #查看数据库连接情况
show index from #表的索引情况
show status #数据库状态查看
SHOW STATUS LIKE '%lock%'; #模糊查询数据库某些状态
SHOW VARIABLES #查看所有配置信息
SHOW variables LIKE '%lock%'; #查看部分配置信息
show engines #查看支持的所有的存储引擎
show engine innodb status\G #查看InnoDB引擎相关的状态信息
show binary logs #列举所有的二进制日志
show master status #查看数据库的日志位置信息
show binlog evnets in #查看二进制日志事件
show slave status \G #查看从库状态
SHOW RELAYLOG EVENTS #查看从库relaylog事件信息
desc (show colums from city) #查看表的列定义信息
http://dev.mysql.com/doc/refman/5.7/en/show.html