当前位置:网站首页>数据库 | 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);
边栏推荐
- MySQL-如何分库分表?一看就懂
- MySQL面试题大全(陆续更新)
- npm WARN config global `--global`, `--local` are deprecated. Use `--location解决方案
- 代码块、Package,Import,封装(第六天)
- gin框架学习-Casbin进阶之策略管理API使用方法
- [Elastic-Job] Overview of Distributed Scheduling Tasks
- Install mysqldb in mac10.14
- Linux modify MySQL database password
- C语言 | 获取字符串里逗号间隔的内容
- leetcode-每日一题731. 我的日程安排表 II
猜你喜欢
随机推荐
Redis管道技术/分区
MySQL compressed package installation, fool teaching
Install mysqldb in mac10.14
feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
win11中利用IIS10搭建asp网站
【Elastic-Job】分布式调度任务概览篇
About integrating superset into your own project
最新MySql安装教学,非常详细
leetcode-每日一题731. 我的日程安排表 II
Eternal blue bug reappears
Why is the redis single-threaded also so fast?
【windows】--- SQL Server 2008 超详细安装教程
"limit" query in Oracle database
C language tutorial (2) - printf and data types that come with c
MySql创建数据表
uni-app进阶之自定义【day13】
Fragmented NFT (Fractional NFT)
On the side of Ali, tell me what are the application scenarios of message middleware you know?
MySql to create data tables
What is the difference between NFT and digital collection?