当前位置:网站首页>数据库 | 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);
边栏推荐
猜你喜欢

Artifact SSMwar exploded Error deploying artifact.See server log for details

代码块、Package,Import,封装(第六天)

Digital twins will be an important way to enter the "metaverse"

GUCCI、LV等奢侈品巨头如何布局元宇宙的,其他品牌应该跟上吗?

一文速学-玩转MySQL获取时间、格式转换各类操作方法详解

利用phpstudy搭建DVWA

leetcode-438. 找到字符串中所有字母异位词(滑动窗口)

Why is the redis single-threaded also so fast?

12 【网页布局总结 元素的显示与隐藏】

On the side of Ali, tell me what are the application scenarios of message middleware you know?
随机推荐
5 methods of MySQL paging query
leetcode-每日一题1252. 奇数值单元格的数目(模拟优化)
C语言 | 获取字符串里逗号间隔的内容
MySQL-如何分库分表?一看就懂
vulhub靶场学习日记hackme2
[JVM Loading]---Class Loading Mechanism
元宇宙的前景及四大赛道
为什么redis是单线程还那么快?
MySQL分页查询的5种方法
(Crypto essential dry goods) Detailed analysis of the current NFT trading markets
What is GameFi?
mysql password modification method in Linux (pro-test available)
find、filter、map的区别
[Elastic-Job] Overview of Distributed Scheduling Tasks
On the side of Ali, tell me what are the application scenarios of message middleware you know?
Build vulhub vulnerability shooting range on kali
wpf ScrowViewer水平滚动
04 【计算属性 侦听属性】
gin框架学习-Gin框架和Gorm框架搭建一个简单的API微服务
GUCCI、LV等奢侈品巨头如何布局元宇宙的,其他品牌应该跟上吗?