[mysql8循序渐进]八 查询DQL
wptr33 2024-11-17 16:44 29 浏览
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如何查询连号
相关推荐
- Linux文件系统操作常用命令(linux文件内容操作命令)
-
在Linux系统中,有一些常用的文件系统操作命令,以下是这些命令的介绍和作用:#切换目录,其中./代表当前目录,../代表上一级目录cd#查看当前目录里的文件和文件夹ls#...
- 别小看tail 命令,它难倒了技术总监
-
我把自己以往的文章汇总成为了Github,欢迎各位大佬star...
- lnav:基于 Linux 的高级控制台日志文件查看器
-
lnav是一款开源的控制台日志文件查看器,专为Linux和Unix-like系统设计。它通过自动检测日志文件的格式,提取时间戳、日志级别等关键信息,并将多个日志文件的内容按时间顺序合并显示,...
- 声明式与命令式代码(声明模式和命令模式)
-
编程范式中的术语和差异信不信由你,你可能已经以开发人员的身份使用了多种编程范例。因为没有什么比用编程理论招待朋友更有趣的了,所以这篇文章可以帮助您认识代码中的流行范例。命令式编程命令式编程是我们从As...
- linux中的常用命令(linux常用命令和作用)
-
linux中的常用命令linux中的命令统称shell命令shell是一个命令行解释器,将用户命令解析为操作系统所能理解的指令,实现用户与操作系统的交互shell终端:我们平时输入命令,执行程序的那个...
- 提高工作效率的--Linux常用命令,能够决解95%以上的问题
-
点击上方关注,第一时间接受干货转发,点赞,收藏,不如一次关注评论区第一条注意查看回复:Linux命令获取linux常用命令大全pdf+Linux命令行大全pdf...
- 如何限制他人操作自己的电脑?(如何控制别人的电脑不让发现)
-
这段时间,小猪罗志祥正处于风口浪尖,具体是为啥?还不知道的小伙伴赶紧去补一下最近的娱乐圈八卦~简单来说,就是我们的小罗同事,以自己超强的体力,以及超强的时间管理能力,重新定义了「多人运动」的含义,重新...
- 最通俗易懂的命令模式讲解(命令模式百科)
-
我们先不讲什么是命令模式,先通过一个场景来引出命令模式,看看命令模式能解决什么样的问题。现在有一个渣男张三,他有还几个女朋友,你现在是不是还是单身狗,你就说你气不气?然后他需要每天分别叫几个女朋友起床...
- 互联网大厂后端必看!Spring Boot 中Runtime执行与停止命令?
-
你是否曾在使用SpringBoot开发项目时,遇到需要执行系统命令的场景?比如调用脚本进行文件处理,又或是启动外部程序?很多后端开发人员会使用Processexec=Runtime.get...
- Linux 常用命令(linux常用的20个命令面试)
-
日志排查类操作命令...
- Java字节码指令:if_icmpgt(0xA3)(java字节码使用的汇编语言)
-
if_icmpgt是Java字节码中的一条条件跳转指令,其全称是"IfIntegerCompareGreaterThan"。它用于比较两个整数值的大小。如果栈顶的第一个...
- 外贸干货|如何增加领英的曝光量和询盘
-
#跨境电商#...
- golang执行linux命令(golang调用shell脚本)
-
需求需要通过openssl生成rsa秘钥,然后保存该秘钥。代码实例packagemainimport("io/ioutil""bytes"&...
- LINUX磁盘挂载(linux磁盘挂载到windows)
-
1、使用root用户查看磁盘挂载情况:fdisk-l2、使用df查看当前磁盘挂载情况,根据和fdisk-l的结果进行对比,查看还有那些磁盘未使用3、挂载:mount磁盘挂载路径...
- Linux命令学习——nl命令(linux ln命令的使用)
-
nl命令主要功能为每一个文件添加行号,每一个输入的文件添加行号后发送到标准输出。当没有文件或文件为-时,读取标准输入...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
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)