[mysql8循序渐进]八 查询DQL
wptr33 2024-11-17 16:44 30 浏览
SELECT语句,mysql的sql语句不区分大小写。表和列可以有别名
1 查询字段
查询所有字段可以用*或者所有列名,推荐后者,多列列名要用英文逗号分开。
select * from 表名;
drop table if exists tb_emp1;
create table tb_emp1(
depid int,
id int,
name varchar(20),
salary double not null,
primary key(depid,id),
constraint uniq_name unique (name)
);
insert into tb_emp1(depid,id,name,salary) values(1,1,'a',100); -- succ
insert into tb_emp1(depid,id,name,salary) values(1,2,null,100); -- succ
select * from tb_emp1;
1 1 a 100 1 2 100
select depid,id,name,salary from tb_emp1;
2 筛选条件
用where过滤
drop table if exists tb_emp1;
create table tb_emp1(
depid int,
id int,
name varchar(20),
salary double not null,
primary key(depid,id),
constraint uniq_name unique (name)
);
insert into tb_emp1(depid,id,name,salary) values(1,1,'a',100); -- succ
insert into tb_emp1(depid,id,name,salary) values(1,2,null,100); -- succ
1 条件判断
select * from tb_emp1 where salary>=100;
1 1 a 100
1 2 100
2 IN
select * from tb_emp1 where id in(1,2);
1 1 a 100
1 2 100
select * from tb_emp1 where id not in(1,3);
1 1 a 100
3 BETWEEN AND
select * from tb_emp1 where id BETWEEN 2 AND 4;
1 2 100
select * from tb_emp1 where id NOT BETWEEN 2 AND 4;
1 1 a 100
4 LIKE
_ 匹配一个任意字符, %匹配0到任意个任意字符,如需转义\,其中\在插入时就要转义
drop table if exists tb_emp1;
create table tb_emp1(
depid int,
id int,
name varchar(20),
salary double not null,
primary key(depid,id),
constraint uniq_name unique (name)
);
insert into tb_emp1(depid,id,name,salary) values(1,1,'a',100); -- succ
insert into tb_emp1(depid,id,name,salary) values(1,2,null,100); -- succ
insert into tb_emp1(depid,id,name,salary) values(2,1,'_a',50); -- succ
insert into tb_emp1(depid,id,name,salary) values(2,2,'%a',200); -- succ
insert into tb_emp1(depid,id,name,salary) values(2,3,'\\a',200); -- succ
select * from tb_emp1 where name like '_a';
depid id name salary
2 1 _a 50
2 2 %a 200
2 3 \a 200
select * from tb_emp1 where name like '\_a';
2 1 _a 50
select * from tb_emp1 where name like '%';
1 1 a 100
2 1 _a 50
2 2 %a 200
2 3 \a 200
select * from tb_emp1 where name = '\\a';
2 3 \a 200
5 查询空值
select * from tb_emp1 where name IS NULL;
1 2 100
select * from tb_emp1 where name IS NOT NULL;
depid id name salary
2 1 _a 50
2 3 \a 200
2 2 %a 200
1 1 a 100
6 AND 可以有多个
select * from tb_emp1 where name IS NOT NULL AND salary>50 AND id=1;
1 1 a 100
7 OR 可以有多个,优先级低于AND
select * from tb_emp1 where name IS NULL OR salary=50 AND id=1;
1 1 a 100
3 DISTINCT 消除重复记录
drop table if exists tb_emp1;
create table tb_emp1(
depid int,
id int,
name varchar(20),
salary double not null,
primary key(depid,id),
constraint uniq_name unique (name)
);
insert into tb_emp1(depid,id,name,salary) values(1,1,'a',100); -- succ
insert into tb_emp1(depid,id,name,salary) values(1,2,null,100); -- succ
insert into tb_emp1(depid,id,name,salary) values(2,1,'_a',100); -- succ
insert into tb_emp1(depid,id,name,salary) values(2,2,'%a',100); -- succ
insert into tb_emp1(depid,id,name,salary) values(2,3,'\\a',100); -- succ
select DISTINCT id,salary from tb_emp1;
1 100
2 100
3 100
4 排序
order by 字段名,字段名 [ASC|DESC]
默认就是ASC,多个字段先排前面的,排完基础上再排后面的。
drop table if exists tb_emp1;
create table tb_emp1(
depid int,
id int,
name varchar(20),
salary double not null,
primary key(depid,id),
constraint uniq_name unique (name)
);
insert into tb_emp1(depid,id,name,salary) values(1,1,'a',10); -- succ
insert into tb_emp1(depid,id,name,salary) values(1,2,null,50); -- succ
insert into tb_emp1(depid,id,name,salary) values(2,1,'_a',30); -- succ
insert into tb_emp1(depid,id,name,salary) values(2,2,'%a',20); -- succ
insert into tb_emp1(depid,id,name,salary) values(2,3,'\\a',40); -- succ
select * from tb_emp1 ORDER BY salary;
1 1 a 10
2 2 %a 20
2 1 _a 30
2 3 \a 40
1 2 50
select * from tb_emp1 ORDER BY depid asc,salary desc;
1 2 50
1 1 a 10
2 3 \a 40
2 1 _a 30
2 2 %a 20
5 分组
group by 字段名,字段名 [having 组条件]
drop table if exists tb_emp1;
create table tb_emp1(
depid int,
id int,
name varchar(20),
salary double not null,
primary key(depid,id),
constraint uniq_name unique (name)
);
insert into tb_emp1(depid,id,name,salary) values(1,1,'a',10); -- succ
insert into tb_emp1(depid,id,name,salary) values(1,2,null,50); -- succ
insert into tb_emp1(depid,id,name,salary) values(2,1,'_a',30); -- succ
insert into tb_emp1(depid,id,name,salary) values(2,2,'%a',20); -- succ
insert into tb_emp1(depid,id,name,salary) values(2,3,'\\a',40); -- succ
select depid,id from tb_emp1 GROUP BY depid,id;
1 2
2 1
2 3
2 2
1 1
select depid,count(*) from tb_emp1 GROUP BY depid;
1 2
2 3
select depid,count(*) from tb_emp1 GROUP BY depid having count(*)>2;
2 3
group_concat 合并组内的一个字段
select depid,group_concat(id) from tb_emp1 GROUP BY depid;
1 1,2
2 1,2,3
with rollup 新加一行统计总和,此时不能有order by了
select depid,count(*) from tb_emp1 GROUP BY depid with rollup ;
1 2
2 3
5
集合函数可以和group一起用,也可以用在全部
count(*)统计总行数,count(字段名)忽略该字段空值。
sum(列)求和
avg(列)求平均
max(列)求最大
min(列)求最小
6 limit
limit [行偏移量,] 行数
行偏移量默认0,第一条记录。
drop table if exists tb_emp1;
create table tb_emp1(
depid int,
id int,
name varchar(20),
salary double not null,
primary key(depid,id),
constraint uniq_name unique (name)
);
insert into tb_emp1(depid,id,name,salary) values(1,1,'a',10); -- succ
insert into tb_emp1(depid,id,name,salary) values(1,2,null,50); -- succ
insert into tb_emp1(depid,id,name,salary) values(2,1,'_a',30); -- succ
insert into tb_emp1(depid,id,name,salary) values(2,2,'%a',20); -- succ
insert into tb_emp1(depid,id,name,salary) values(2,3,'\\a',40); -- succ
select depid,id from tb_emp1 limit 2;
1 2
2 1
select depid,id from tb_emp1 limit 0,2;
1 2
2 1
select depid,id from tb_emp1 limit 1,2;
2 1
2 3
7 内连接
inner join
drop table if exists tb_emp1;
create table tb_emp1(
id int primary key,
name varchar(10)
);
drop table if exists tb_part1;
create table tb_part1(
pid int,
id int,
dname varchar(10),
primary key(pid,id)
);
insert into tb_emp1(id,name) values('1','a');
insert into tb_emp1(id,name) values('2','b');
insert into tb_emp1(id,name) values('3','c');
insert into tb_emp1(id,name) values('4','d');
insert into tb_part1(pid,id,dname) values('11','1','aa');
insert into tb_part1(pid,id,dname) values('11','2','aa');
insert into tb_part1(pid,id,dname) values('12','3','bb');
insert into tb_part1(pid,id,dname) values('13','5','cc');
select t1.id,t1.name,t2.pid,t2.dname from tb_emp1 t1, tb_part1 t2 where t1.id=t2.id
1 a 11 aa
2 b 11 aa
3 c 12 bb
select t1.id,t1.name,t2.pid,t2.dname from tb_emp1 t1 inner join tb_part1 t2 on t1.id=t2.id
1 a 11 aa
2 b 11 aa
3 c 12 bb
8 左连接
left join 左表所有行出现,若右表没有则右表列为空
select t1.id,t1.name,t2.pid,t2.dname from tb_emp1 t1 left join tb_part1 t2 on t1.id=t2.id;
1 a 11 aa
2 b 11 aa
3 c 12 bb
4 d
9 右连接
right join 右表所有行出现,若左表没有则左表列为空
select t1.id,t1.name,t2.pid,t2.dname from tb_emp1 t1 right join tb_part1 t2 on t1.id=t2.id;
1 a 11 aa
2 b 11 aa
3 c 12 bb
13 cc
10 子查询
any(some) 与子查询任何值比较为true则为true
select t1.id,t1.name from tb_emp1 t1 where t1.id = any(select id from tb_part1)
1 a
2 b
3 c
all 要满足所有子查询条件
select t1.id,t1.name from tb_emp1 t1 where t1.id <= all (select id from tb_part1)
1 a
exists 子查询能至少返回一行,NOT exists相反
select t1.id,t1.name from tb_emp1 t1 where exists(select t2.id from tb_part1 t2 where t1.id=t2.id)
1 a
2 b
3 c
in 子查询的结果为外层的比较条件;not in 相反
select t1.id,t1.name from tb_emp1 t1 where t1.id in (select t2.id from tb_part1 t2)
1 a
2 b
3 c
11 合并查询结果/h3>
union 会删除重复记录,union不删除
select t1.id from tb_emp1 t1 where t1.id in('1','2')
union
select t1.id from tb_emp1 t1 where t1.id in('1','3')
1
2
3
select t1.id from tb_emp1 t1 where t1.id in('1','2')
union all
select t1.id from tb_emp1 t1 where t1.id in('1','3')
id 1
2
1
3
12 正则表达式/h3>
mysql是regexp
select t1.pid,t1.dname from tb_part1 t1 where t1.dname regexp '^a|b'
11 aa
11 aa
12 bb
13 通用表达式
cte:common table expressions,可以复用的子查询,可以在select/update/delete前面
with cte as(select t1.id,t1.name from tb_emp1 t1 )
select t2.pid,t2.dname,cte.id,cte.name from tb_part1 t2,cte where t2.id=cte.id
11 aa 1 a
11 aa 2 b
12 bb 3 c
- 上一篇:mysql查询指定父级下所有子级
- 下一篇:MySQL如何查询连号
相关推荐
- Python自动化脚本应用与示例(python办公自动化脚本)
-
Python是编写自动化脚本的绝佳选择,因其语法简洁、库丰富且跨平台兼容性强。以下是Python自动化脚本的常见应用场景及示例,帮助你快速上手:一、常见自动化场景文件与目录操作...
- Python文件操作常用库高级应用教程
-
本文是在前面《Python文件操作常用库使用教程》的基础上,进一步学习Python文件操作库的高级应用。一、高级文件系统监控1.1watchdog库-实时文件系统监控安装与基本使用:...
- Python办公自动化系列篇之六:文件系统与操作系统任务
-
作为高效办公自动化领域的主流编程语言,Python凭借其优雅的语法结构、完善的技术生态及成熟的第三方工具库集合,已成为企业数字化转型过程中提升运营效率的理想选择。该语言在结构化数据处理、自动化文档生成...
- 14《Python 办公自动化教程》os 模块操作文件与文件夹
-
在日常工作中,我们经常会和文件、文件夹打交道,比如将服务器上指定目录下文件进行归档,或将爬虫爬取的数据根据时间创建对应的文件夹/文件,如果这些还依靠手动来进行操作,无疑是费时费力的,这时候Pyt...
- python中os模块详解(python os.path模块)
-
os模块是Python标准库中的一个模块,它提供了与操作系统交互的方法。使用os模块可以方便地执行许多常见的系统任务,如文件和目录操作、进程管理、环境变量管理等。下面是os模块中一些常用的函数和方法:...
- 21-Python-文件操作(python文件的操作步骤)
-
在Python中,文件操作是非常重要的一部分,它允许我们读取、写入和修改文件。下面将详细讲解Python文件操作的各个方面,并给出相应的示例。1-打开文件...
- 轻松玩转Python文件操作:移动、删除
-
哈喽,大家好,我是木头左!Python文件操作基础在处理计算机文件时,经常需要执行如移动和删除等基本操作。Python提供了一些内置的库来帮助完成这些任务,其中最常用的就是os模块和shutil模块。...
- Python 初学者练习:删除文件和文件夹
-
在本教程中,你将学习如何在Python中删除文件和文件夹。使用os.remove()函数删除文件...
- 引人遐想,用 Python 获取你想要的“某个人”摄像头照片
-
仅用来学习,希望给你们有提供到学习上的作用。1.安装库需要安装python3.5以上版本,在官网下载即可。然后安装库opencv-python,安装方式为打开终端输入命令行。...
- Python如何使用临时文件和目录(python目录下文件)
-
在某些项目中,有时候会有大量的临时数据,比如各种日志,这时候我们要做数据分析,并把最后的结果储存起来,这些大量的临时数据如果常驻内存,将消耗大量内存资源,我们可以使用临时文件,存储这些临时数据。使用标...
- Linux 下海量文件删除方法效率对比,最慢的竟然是 rm
-
Linux下海量文件删除方法效率对比,本次参赛选手一共6位,分别是:rm、find、findwithdelete、rsync、Python、Perl.首先建立50万个文件$testfor...
- Python 开发工程师必会的 5 个系统命令操作库
-
当我们需要编写自动化脚本、部署工具、监控程序时,熟练操作系统命令几乎是必备技能。今天就来聊聊我在实际项目中高频使用的5个系统命令操作库,这些可都是能让你效率翻倍的"瑞士军刀"。一...
- Python常用文件操作库使用详解(python文件操作选项)
-
Python生态系统提供了丰富的文件操作库,可以处理各种复杂的文件操作需求。本教程将介绍Python中最常用的文件操作库及其实际应用。一、标准库核心模块1.1os模块-操作系统接口主要功能...
- 11. 文件与IO操作(文件io和网络io)
-
本章深入探讨Go语言文件处理与IO操作的核心技术,结合高性能实践与安全规范,提供企业级解决方案。11.1文件读写11.1.1基础操作...
- Python os模块的20个应用实例(python中 import os模块用法)
-
在Python中,...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)