百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT技术 > 正文

[mysql8循序渐进]八 查询DQL

wptr33 2024-11-17 16:44 22 浏览

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

相关推荐

【推荐】一款开源免费、美观实用的后台管理系统模版

如果您对源码&技术感兴趣,请点赞+收藏+转发+关注,大家的支持是我分享最大的动力!!!项目介绍...

Android架构组件-App架构指南,你还不收藏嘛

本指南适用于那些已经拥有开发Android应用基础知识的开发人员,现在想了解能够开发出更加健壮、优质的应用程序架构。首先需要说明的是:AndroidArchitectureComponents翻...

高德地图经纬度坐标批量拾取(高德地图批量查询经纬度)

使用方法在桌面上新建一个index.txt文件,把下面的代码复制进去保存,再把文件名改成index.html保存,双击运行打开即可...

flutter系列之:UI layout简介(flutter ui设计)

简介对于一个前端框架来说,除了各个组件之外,最重要的就是将这些组件进行连接的布局了。布局的英文名叫做layout,就是用来描述如何将组件进行摆放的一个约束。...

Android开发基础入门(一):UI与基础控件

Android基础入门前言:...

iOS的布局体系-流式布局MyFlowLayout

iOS布局体系的概览在我的CSDN博客中的几篇文章分别介绍MyLayout布局体系中的视图从一个方向依次排列的线性布局(MyLinearLayout)、视图层叠且停靠于父布局视图某个位置的框架布局(M...

TDesign企业级开源设计系统越发成熟稳定,支持 Vue3 / 小程序

TDesing发展越来越好了,出了好几套组件库,很成熟稳定了,新项目完全可以考虑使用。...

WinForm实现窗体自适应缩放(winform窗口缩放)

众所周知,...

winform项目——仿QQ即时通讯程序03:搭建登录界面

上两篇文章已经对CIM仿QQ即时通讯项目进行了需求分析和数据库设计。winform项目——仿QQ即时通讯程序01:原理及项目分析...

App自动化测试|原生app元素定位方法

元素定位方法介绍及应用Appium方法定位原生app元素...

61.C# TableLayoutPanel控件(c# tabcontrol)

摘要TableLayoutPanel在网格中排列内容,提供类似于HTML元素的功能。TableLayoutPanel控件允许你将控件放在网格布局中,而无需精确指定每个控件的位置。其单元格...

想要深入学习Android性能优化?看完这篇直接让你一步到位

...

12个python数据处理常用内置函数(python 的内置函数)

在python数据分析中,经常需要对字符串进行各种处理,例如拼接字符串、检索字符串等。下面我将对python中常用的内置字符串操作函数进行介绍。1.计算字符串的长度-len()函数str1='我爱py...

如何用Python程序将几十个PDF文件合并成一个PDF?其实只要这四步

假定你有一个很无聊的任务,需要将几十个PDF文件合并成一个PDF文件。每一个文件都有一个封面作为第一页,但你不希望合并后的文件中重复出现这些封面。即使有许多免费的程序可以合并PDF,很多也只是简单的将...

Python入门知识点总结,Python三大数据类型、数据结构、控制流

Python基础的重要性不言而喻,是每一个入门Python学习者所必备的知识点,作为Python入门,这部分知识点显得很庞杂,内容分支很多,大部分同学在刚刚学习时一头雾水。...