当前位置:网站首页>数据库 | SQL查询进阶语法
数据库 | SQL查询进阶语法
2022-07-31 05:11:00 【Benni-King】
在test这个数据库中,增加了两个table
一个叫students
一个叫classes
具体内容如下
mysql> select * from students;
+----+-----------+------+--------+--------+--------+-----------+
| id | name | age | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
| 15 | 小明 | 18 | 180.00 | 女 | 1 | |
| 16 | 小月月 | 18 | 180.00 | 女 | 2 | |
| 17 | 彭于晏 | 29 | 185.00 | 男 | 1 | |
| 18 | 刘德华 | 59 | 175.00 | 男 | 2 | |
| 19 | 黄蓉 | 38 | 160.00 | 女 | 1 | |
| 20 | 凤姐 | 28 | 150.00 | 保密 | 2 | |
| 21 | 王祖贤 | 18 | 172.00 | 女 | 1 | |
| 22 | 周杰伦 | 36 | NULL | 男 | 1 | |
| 23 | 程萧 | 27 | 181.00 | 男 | 2 | |
| 24 | 刘亦菲 | 25 | 166.00 | 女 | 2 | |
| 25 | 金星 | 33 | 162.00 | 中性 | 3 | |
| 26 | 静香 | 12 | 180.00 | 女 | 4 | |
| 27 | 郭靖 | 12 | 170.00 | 男 | 4 | |
| 28 | 周杰 | 34 | 176.00 | 女 | 5 | |
+----+-----------+------+--------+--------+--------+-----------+
mysql> select * from classes;
+----+----------------+
| id | name |
+----+----------------+
| 1 | python01_times |
| 2 | python02_times |
| 3 | python03_times |
+----+----------------+
1.查
as 给表起表名或者给列起表名
select s.name s.age from students as s;
select name as 名字, age as 年龄 from students;
select distinct gender from students;
1.1条件查询
select * from students where age<18 and age<30;
select * from students where age>20 and gender="男";
select * from students where age>25 or height>170;
取反集
select * from students where not (age>20 and gender="男");
1.2 模糊查询
like
select name from students where name="小";
select name from studnets where name like "小%";
--查询名字里面有小的所有名字
select name from students where name like "%小%";
--查询名字里面有3个字的名字
select name from students where name like "__";
--查询名字里面至少两个字以上的
select name from students where name like "__%";
rlike
-- rlike 正则
-- 查询以 周开始的姓名
select name from students where name rlike "^周*";
--查询以周开始伦结尾的姓名
select name from students where name rlike "周.*伦$";
1.3 范围查询,null
select name,age from students where age in (18, 29 ,59);
select name,age from students where age not between 19 and 30;
select * from students where height is null ;
1.4 升序降序
1.4.1单个
默认就是升序,降序需要指令desc指明
默认就是升序,所以升序可以省略,降序需要另外指定
select * from students where (age not between 18 and 30 ) and gender = 1 order by age;
select * from students where (age not between 18 and 30 ) and gender = 1 order by age asc;
select * from students where (age not between 18 and 30 ) and gender = 1 order by age desc;
1.4.2 多列
就近原则,最靠近order by的条件优先级最高,先排序,若相同,再看第二个条件
select * from students where (age not between 18 and 30 ) and gender = 1 order by age asc , id desc;
1.5 聚合,分组
select count(*) as 性别 from students where gender=1;
select max(age) as 最大年龄 from students;
select round(sum(age)/count(*),2) from students;
select gender, avg(age) from students group by gender;
select gender, group_concat(name),avg(age) from students group by gender;
select gender, group_concat(name,"_",age,"_",id),avg(age) from students g
roup by gender;
select gender, group_concat(name) from students group by gender having count(*)>3;
select gender , group_concat(name) from students group by gender having avg(age)>30;
where 和 having的区别,
- where在group by 前面,having在group by 后面
- where是对原始数据进行限制
- having是对查询的结果进行限制
1.6 分页(limit)
展示两个
select * from students limit 2;
从第十个开始展示5个
select * from students limit 10,5;
select * from students where gender=2 order by age desc limit 2;
limit 写在order by后面
1.7 连接查询
内连接
select * from students inner join classes on students.cls_id=classes.id;
select students.name ,classes.name from students inner join classes on students.cls_id = classes.id;
外连接
外连接的就有 left join 和 right join两个,但一般用的是left join,right join很少用
原理:就是基于left join语句左边的的这张表查询右边这张表有无对应的条件,有显示数据,没有显示为null,right join 就是把表位置互换,所以一般用left join。
select students.name as 姓名 , classes.name as 班级 from students left join classes on students.cls_id=classes.id having classes.name is null
1.8 自关联
就是表里面的一列的字段关联另一列字段,比如省市县、公司上下级
1.9 子查询
select * from students where height = (select max(height) from students);
边栏推荐
猜你喜欢

【Elastic-Job源码分析】——作业监听器

gin框架学习-Gin框架和Gorm框架搭建一个简单的API微服务

阿里云中mysql数据库被攻击了,最终数据找回来了

利用phpstudy搭建DVWA

永恒之蓝漏洞复现

Build DVWA with phpstudy

DeFi Token in the project management

vulhub靶场学习日记hackme2

Error: Cannot find module 'D:\Application\nodejs\node_modules\npm\bin\npm-cli.js'

【云原生】原来2020.0.X版本开始的OpenFeign底层不再使用Ribbon了
随机推荐
闭包(五)----一个常见的循环
闭包(二)
代码执行漏洞
Error: Cannot find module ‘D:\Application\nodejs\node_modules\npm\bin\npm-cli.js‘
MySQL高级SQL语句(二)
leetcode-1833. 雪糕的最大数量(排序+贪心)
uni-app进阶之内嵌应用【day14】
find、filter、map的区别
12 【网页布局总结 元素的显示与隐藏】
【云原生】微服务Nacos的简单介绍与使用
MySQL面试题大全(陆续更新)
2021面经-拥抱变化
DeFi 项目中的治理Token
字符串的新增方法
12 【nextTick 过渡与动画】
MySQL-如何分库分表?一看就懂
MySql to create data tables
MySQL压缩包方式安装,傻瓜式教学
leetcode-每日一题1217. 玩筹码(贪心+位运算)
leetcode-每日一题731. 我的日程安排表 II