博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2sql
阅读量:5829 次
发布时间:2019-06-18

本文共 5240 字,大约阅读时间需要 17 分钟。

------------------------------------ 高级查询

-- as 起别名
select name as 名字 from studnets;
-- 消除重复的行 -- 查看有哪几种xxx
select distinct gender from students; -- 查看有哪几种性别
select distinct name,gender from students; -- 查看有哪几种(性别,姓名)组合

------ where 运算符

---- 比较
-- 等于: =
-- 大于: >
-- 大于等于: >=
-- 小于: <
-- 小于等于: <=
-- 不等于: != 或 <>
select * from students where id <= 4;
---- 逻辑
-- and or not
select * from students where id < 4 or is_delete=0;
-------- 模糊查询
-- like
-- %表示任意多个任意字符
-- _表示一个任意字符
select * from students where name like 'M%';
select * from students where name like 'Mik_';
-------- 范围查询
-- in表示在一个非连续的范围内
select * from students where id in (1, 3);
-- between ... and ...表示在一个连续的范围内
select * from students where cls_id between 1 and 3; -- 1,2,3
--------- 空判断
select * from students where cls_id is not null;

--------------------------------------------------------排序
select * from students order by name asc默认/desc;
select * from students [where age in (10, 11)] order by name; -- 先拿到数据集再排序

--------------------------------------------------------聚合函数

select count(*) from students;
-- select max min avg
select sum(age) from students;

-------------------------------------------------------- 分组

select gender from students group by gender;
-- +--------+
-- | gender |
-- +--------+
-- | 男 |
-- | 女 |
-- | 中性 |
-- | 保密 |
-- +--------+
-------------------- group by + group_concat()
select gender, group_concat(name) from students group by gender;
-- +--------+-----------------------------------------------------------+
-- | gender | group_concat(name) |
-- +--------+-----------------------------------------------------------+
-- | 男 | 彭于晏,刘德华,周杰伦,程坤,郭靖 |
-- | 女 | 小明,小月月,黄蓉,王祖贤,刘亦菲,静香,周杰 |
-- | 中性 | 金星 |
-- | 保密 | 凤姐 |
-- +--------+-----------------------------------------------------------+

-------------------- group by + 集合函数
-- 分别统计性别为男/女的人年龄平均值
select gender,avg(age) from students group by gender;

-------------------- group by + having 过滤
-- 平均年龄大于10的性别
select gender, avg(age) from students group by gender having avg(age) > 10;
-- 人数大于1的性别和人数
select gender, count(*) from students group by gender having count(*) > 1;

-------------------- group by + with rollup 汇总

---- with rollup的作用是:在最后新增一行,来记录当前列里所有记录的总和
select gender,count(*) from students group by gender with rollup;

----------------------------------------分页

select * from students limit 3; -- 取前3条,相当于0,3
---- select * from 表名 limit start,count
select * from students limit 2, 4; -- 跳过2条 从第3条开始取4条

---------------------------------------- 链接查询

select * from classes as c [inner] join students as s on c.id = s.cls_id;
-- +----+-----------+----+------+------+--------+--------+--------+
-- | id | name | id | name | age | height | gender | cls_id |
-- +----+-----------+----+------+------+--------+--------+--------+
-- | 1 | 精英班 | 1 | Mike | 10 | 170.00 | 男 | 1 |
-- | 1 | 精英班 | 2 | John | 11 | 180.00 | 男 | 1 |
-- +----+-----------+----+------+------+--------+--------+--------+
select * from classes as c left join students as s on c.id = s.cls_id;
-- +----+-----------+------+------+------+--------+--------+--------+
-- | id | name | id | name | age | height | gender | cls_id |
-- +----+-----------+------+------+------+--------+--------+--------+
-- | 1 | 精英班 | 1 | Mike | 10 | 170.00 | 男 | 1 |
-- | 1 | 精英班 | 2 | John | 11 | 180.00 | 男 | 1 |
-- +----+-----------+------+------+------+--------+--------+--------+
select * from classes as c right join students as s on c.id=s.cls_id;
-- +------+-----------+----+------+------+--------+--------+--------+
-- | id | name | id | name | age | height | gender | cls_id |
-- +------+-----------+----+------+------+--------+--------+--------+
-- | 1 | 精英班 | 1 | Mike | 10 | 170.00 | 男 | 1 |
-- | 1 | 精英班 | 2 | John | 11 | 180.00 | 男 | 1 |
-- +------+-----------+----+------+------+--------+--------+--------+

---------------------------------------自关联

-- 通常大分类有小分类这种形式的数据放到一个表中,并且pid指向表的id
CREATE TABLE `areainfo` (
`id` int(10) unsigned NOT NULL,
`name` varchar(32) DEFAULT NULL,
`pid` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
)
--- 查询广东省下的所有地级市
select c.name from areainfo as p inner join areainfo as c
on c.pid=p.id where p.name='广东省';

--- 查询山东省下的所有地级市和县区

-- 注意: [a join b on 条件] 结果是一个`表`********************

select city.name, county.name from areainfo as county join

(areainfo as city join areainfo as province
on city.pid=province.id and province.pid is null)
on city.id=county.pid
where province.name='山东省';

select city.name, county.name from (areainfo as county join

(areainfo as city join areainfo as province
on city.pid=province.id and province.pid is null)
on city.id=county.pid)
where province.name='山东省';

---------------------------------- 子查询
--- 每个SQL包含两部分 主查询 和 子查询
-- 子查询有三种类型
-- 标量子查询: 子查询返回的结果是一个数据(一行一列)
-- 列子查询: 返回的结果是一列(一列多行)
-- 行子查询: 返回的结果是一行(一行多列)

-- 标量子查询: 将子查询的结果当成一个值
-- 查询大于平均年龄的学生
select * from students where age > (select avg(age) from students);

-- 列子查询: 将子查询的结果当成同一属性(列)多个值的的集合

-- 查询班级还存在的学生的名字
select name from students where cls_id in (select id from classes);

-- 行子查询: 将多列数据看成一条数据

-- 查找班级年龄最大,身高最高的学生
select * from students where (height, age)=
(select max(height), max(age) from students);
-- 只有在最大身高、最大年龄刚好是一个人的时候才能查找到数据。

转载于:https://www.cnblogs.com/weiwei-python/p/9781240.html

你可能感兴趣的文章
SAP被评为“大数据”预测分析领军企业
查看>>
联想企业网盘张跃华:让文件创造业务价值
查看>>
记录一次蚂蚁金服前端电话面试
查看>>
直播源码开发视频直播平台,不得不了解的流程
查看>>
Ubuntu上的pycrypto给出了编译器错误
查看>>
聊聊flink的RestClientConfiguration
查看>>
在CentOS上搭建git仓库服务器以及mac端进行克隆和提交到远程git仓库
查看>>
測試文章
查看>>
Flex很难?一文就足够了
查看>>
【BATJ面试必会】JAVA面试到底需要掌握什么?【上】
查看>>
CollabNet_Subversion小结
查看>>
mysql定时备份自动上传
查看>>
Linux 高可用集群解决方案
查看>>
17岁时少年决定把海洋洗干净,现在21岁的他做到了
查看>>
linux 启动oracle
查看>>
《写给大忙人看的java se 8》笔记
查看>>
倒计时:计算时间差
查看>>
Linux/windows P2V VMWare ESXi
查看>>
Windows XP倒计时到底意味着什么?
查看>>
tomcat一步步实现反向代理、负载均衡、内存复制
查看>>