1. select 查询
1.1 select语句单独使用
(1) 查询数据库的参数.
SELECT @ @port;
SELECT @@datadir;
SELECT @ @basedir;
SELECT @@innodb_flush_log_at_trx_commit;
使用concat将输出结果拼接。
concat的一般用法主要是用于拼接
将列换成行显示输出
SELECT GROUP CONCAT (xid) FROM student;
(2) 调用内置函数.
USE oldguo
SELECT DATABASE();
SELECT NOW();
SELECT CONCAT(USER,"@",HOST) FROM mysql.user;
SELECT GROUP_CONCAT(xid) FROM student;
SELECT SUM(xid) FROM student;
列出所有参数,过滤出要使用的
SHOW VARIABLES LIKE '%trx%';
(3) 简易计算器
SELECT 4*5;
1.2 SELECT 配合其他子句使用 *****
1.2.1 子句列表介绍
FROM -- 查询对象(表,视图)
WHERE -- 过滤子句(grep)
GROUP BY -- 分组子句(统计分析类)
HAVING -- 后过滤子句
ORDER BY -- 排序子句
LIMIT -- 限制子句(分页子句)
1.2.2 配合FROM应用
-- world 模板库介绍
--- 英文单词介绍
--- city # 城市
--- id # 序号ID主键
--- NAME # 城市名
--- Countrycode # 国家代码(CHN,USA,JPN)
--- District # 省,州
--- Population #城市人口数
例子:
1.查看表中所有数据
SELECT * FROM city;
2. 过滤name和population列的信息 (awk取列)
SELECT name,population FROM city;
1.2.3 select+ from + where(grep)使用
where 配合等值查询
-- 例子:
1. 查询中国所有的城市信息
SELECT * FROM city
WHERE countrycode='CHN';
2. 查询ID为100的城市信息
SELECT * FROM city
WHERE id=100;
3. 查询 中国河北省的城市信息
SELECT * FROM city
WHERE countrycode='CHN' AND district='hebei' ;
4. 查询 中国或者美国的城市
SELECT * FROM city
WHERE countrycode='CHN' OR countrycode='USA';
或者:
SELECT * FROM city
WHERE countrycode IN ('CHN','USA');
或者:
将两个查询的结果合并显示
SELECT * FROM city
WHERE countrycode='CHN'
UNION ALL
SELECT * FROM city
WHERE countrycode='USA' ;
where 配合范围查询
-- 例子 :
1. 查询人口数量小于100人的城市
SELECT * FROM city
WHERE population<100;
2. 查询人口数量100w-200w之间的
SELECT * FROM city
WHERE population>=1000000 AND population<=2000000 ;
或者:
SELECT * FROM city
WHERE population BETWEEN 1000000 AND 2000000 ;
3. 查询国家代号是CH开头的城市信息
SELECT * FROM city
WHERE countrycode LIKE 'CH%';
1.2.4 group by 分组子句+聚合(统计)函数应用
-- 聚合函数?
COUNT() -- 计数(统计数量)
SUM() -- 求和
AVG() -- 求平均值
MAX() -- 求最大值
MIN() -- 最小值
GROUP_CONCAT() -- 聚合列值
-- 结果集显示特点: 数据成行显示。必须是1v1,不能是1vN
-- 例子 :
1. 统计一下每个国家的人口总数
SELECT countrycode,SUM(population)
FROM city
GROUP BY countrycode;
2. 统计下中国每个省的城市个数及城市名.
SELECT district,COUNT(NAME),GROUP_CONCAT(NAME) FROM city
WHERE countrycode='CHN'
GROUP BY district;
SELECT district,SUM(population) #4. 计算 5. 显示district列及计算结果 | SELECT +要查询的列 district,
FROM city #1.对哪个表操作
WHERE countrycode='CHN' #2.匹配条件
GROUP BY District; #3.分组
查询结果要1对1
group by 执行流程
先读取表的数据--->条件语句where范围读取数据--->group by 语句对信息分组---> 过滤出值,聚合函数运算--->输出结果
3.统计中国每个省的个数,及城市名
SELECT district,COUNT(NAME),GROUP_CONCAT(NAME) FROM city
WHERE countrycode='CHN'
GROUP BY district;
4. 统计每个国家城市个数
SELECT countrycode ,COUNT(NAME) FROM city
GROUP BY countrycode;
1.2.6 having 后判断
1. 统计中国每个省的人口总数,只显示总人口数大于500w的省信息.
SELECT district,SUM(population) FROM city
WHERE countrycode='CHN'
GROUP BY district
HAVING SUM(population) >=5000000;
1.2.7 order by 排序子句
-- 例子:
1. 查询中国所有城市信息,人口数从大到小排序输出.
SELECT * FROM city
WHERE countrycode='CHN'
ORDER BY population DESC ;
2. 查询中国所有城市信息,按城市名排序.
SELECT * FROM city
WHERE countrycode='CHN'
ORDER BY NAME;
3. 查询中国所有省的总人口,并按总人口数从大到小排序输出.
SELECT district,SUM(population) FROM city
WHERE countrycode='CHN'
GROUP BY district
ORDER BY SUM(population) DESC;
1.2.8 limit 分页限制子句
查询中国所有省的总人口,并按总人口数从大到小排序输出.
SELECT district,SUM(population) FROM city
WHERE countrycode='CHN'
GROUP BY district
ORDER BY SUM(population) DESC
LIMIT 5 OFFSET 1;
2-11行
SELECT district,SUM(population) FROM city
WHERE countrycode='CHN'
GROUP BY district
ORDER BY SUM(population) DESC
LIMIT 10;
显示2-6列
SELECT district,SUM(population) FROM city
WHERE countrycode='CHN'
GROUP BY district
ORDER BY SUM(population) DESC
LIMIT 1,5;
注意: LIMIT 谨慎使用, 500w+的表.
LIMIT 5000000,100
一般会改为明确查找范围