[mysql8循序渐进]八 查询DQL
wptr33 2024-11-17 16:44 43 浏览
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如何查询连号
相关推荐
- 第 28 章:核心功能 SQL 查询 - PostgreSQL入门
-
欢迎来到我们史诗级教程的最终章!在上一章,我们成功地构建了博客系统的数据库骨架。现在,这个结构精良的数据库正静静地等待着我们去使用它。...
- postgresql的6种索引介绍_postgresql默认用户名和密码
-
postgresql几种索引PostgreSQL支持多种索引类型,每种索引的设计原理、适用场景和优缺点各有不同。以下是对主要索引类型的详细介绍:...
- 第 20 章:索引与性能优化 - PostgreSQL入门
-
到目前为止,我们已经学习了如何设计表、保证数据完整性、以及如何用各种方式查询数据。但当我们的表从几十行增长到几百万、甚至上亿行时,一个之前只需要0.1秒的查询,可能会变成需要几分钟甚至几小时的“灾...
- PostgreSQL 主从复制 完整指南_主从复制mysql
-
PostgreSQL主从复制(StreamingReplication)完整指南PostgreSQL主从复制是一种实时同步数据的机制,可以实现高可用性(HA)、读写分离和负载均衡。其...
- PostgreSQL监控神器,千万注意这5大关键指标!
-
PostgreSQL监控神器,千万注意这5大关键指标!在当今数据驱动的业务环境中,数据库的性能和稳定性直接关系到企业的运营效率与用户体验。PostgreSQL作为一款功能强大的开源关系型数据库,被广泛...
- Retool 如何升级主应用 4TB 的 PostgreSQL 数据库
-
本文最初发布于Retool官方博客。...
- PostgreSQL查询计划_postgresql查询计划中的cost组成
-
深入解析PostgreSQL查询计划:优化性能的关键在数据库管理系统中,查询计划是执行SQL查询时的关键组成部分。PostgreSQL作为一款功能强大的开源关系型数据库,其查询计划的生成与优化对于提升...
- 第 27 章:数据库与表结构实现 - PostgreSQL入门
-
在上一章,我们已经绘制好了博客系统的宏伟蓝图。现在,是时候戴上安全帽,化身“建筑工程师”,将图纸上的设计一砖一瓦地搭建成真实的数据库结构了。...
- PostgreSQL事务处理_postgresql时区问题
-
PostgreSQL事务处理:原理、应用与优化引言...
- 第 14 章:集合运算 (UNION, INTERSECT, EXCEPT) - PostgreSQL入门
-
在之前的章节里,我们所有的操作(JOIN...
- PostgreSQL 安装指南及日常使用_postgresql 11安装
-
PostgreSQL安装与日常使用PostgreSQL是一款功能强大、开源的对象关系型数据库,支持高级SQL标准、扩展功能、事务完整性和高并发。本指南涵盖安装、配置、日常使用、性能优化、常见...
- 第 23 章:函数与存储过程 (PL/pgSQL) - PostgreSQL入门
-
到目前为止,我们与数据库的交互方式都是从外部客户端(如psql...
- PostgreSQL是不是你的下一个JSON数据库?
-
根据Betteridge定律(任何头条的设问句可以用一个词来回答:不是),除非你的JSON数据很少修改,并且查询很多。最新版的PostgreSQL添加更多对JSON的支持,我们曾经问过PostgreS...
- "揭秘PostgreSQL:你必须掌握的数据类型全解析!"
-
揭秘PostgreSQL:你必须掌握的数据类型全解析!在数据库管理系统中,PostgreSQL以其强大的功能和稳定性而著称。为了充分发挥其性能,理解并熟练掌握其数据类型是至关重要的。本文将深入探讨Po...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
程序员的开源月刊《HelloGitHub》第 71 期
-
假如有100W个用户抢一张票,除了负载均衡办法,怎么支持高并发?
-
详细介绍一下Redis的Watch机制,可以利用Watch机制来做什么?
-
Java面试必考问题:什么是乐观锁与悲观锁
-
如何将AI助手接入微信(打开ai手机助手)
-
redission YYDS spring boot redission 使用
-
SparkSQL——DataFrame的创建与使用
-
一文带你了解Redis与Memcached? redis与memcached的区别
-
如何利用Redis进行事务处理呢? 如何利用redis进行事务处理呢英文
-
- 最近发表
-
- 第 28 章:核心功能 SQL 查询 - PostgreSQL入门
- postgresql的6种索引介绍_postgresql默认用户名和密码
- 第 20 章:索引与性能优化 - PostgreSQL入门
- PostgreSQL 主从复制 完整指南_主从复制mysql
- PostgreSQL监控神器,千万注意这5大关键指标!
- Retool 如何升级主应用 4TB 的 PostgreSQL 数据库
- PostgreSQL查询计划_postgresql查询计划中的cost组成
- 第 27 章:数据库与表结构实现 - PostgreSQL入门
- 谁帮我看看,为啥我的PostgreSQL查询速度这么慢???
- PostgreSQL事务处理_postgresql时区问题
- 标签列表
-
- 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)