mysql-DQL数据查询语句

DQL数据查询语句

1.select

1.1 select 单独使用

(1)查询数据库的参数

-- select @@xxx 查看系统参数
SELECT @@port; --查询端口
SELECT @@basedir;
SELECT @@datadir;
SELECT @@socket;
SELECT @@server_id;-- select @@xxx 查看系统参数

select @@port; 查询端口

SHOW VARIABLES LIKE '%trx%' --查询所有参数,模糊匹配

(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;

(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.查询表中所有数据(cat)
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' ;

-- 3. 查询国家代号是CH开头的城市信息
SELECT * FROM city
WHERE countrycode LIKE 'CH%';

1.3.2 where配合比较操作符(> < >= <= <>)
例子:
-- 查询世界上少于100人的城市
SELECT * FROM city WHERE population<100;
1.3.3 where配合逻辑运算符(and or )
例子:
-- 中国人口数量大于500w
SELECT * FROM city WHERE countrycode='CHN' AND population>5000000;
-- 中国或美国城市信息
SELECT * FROM city WHERE countrycode='CHN' OR countrycode='USA';
1.3.4 where配合模糊查询
例子:
-- 查询省的名字前面带guang开头的
SELECT * FROM city WHERE district LIKE 'guang%';
注意:%不能放在前面,因为不走索引.

1.3.5 where配合in语句
-- 中国或美国城市信息
SELECT * FROM city WHERE countrycode IN ('CHN' ,'USA');

1.3.6 where配合between and
例子:
-- 查询世界上人口数量大于100w小于200w的城市信息
SELECT * FROM city WHERE population >1000000 AND population <2000000;
SELECT * FROM city WHERE population BETWEEN 1000000 AND 2000000;

-- 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,SUM(population) FROM city
WHERE countrycode='CHN'
GROUP BY district;

-- 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;

SELECT district,SUM(population) FROM city
WHERE countrycode='CHN'
GROUP BY district
ORDER BY SUM(population) DESC
LIMIT 10;

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
一般会改为明确查找范围

总结使用

SELECT District,SUM(population)
FROM city
WHERE countrycode='chn'
GROUP BY District
HAVING SUM(population) >=5000000
ORDER BY SUM(population) DESC
LIMIT 1,3;

## distinct:

去重复

SELECT countrycode FROM city ;
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 不去重复

你可能感兴趣的:(mysql-DQL数据查询语句)