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

[mysql8循序渐进]八 查询DQL

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

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

相关推荐

python生成脚本与部署的方案(python生成脚本与部署的方案区别)

上周接到一个需求任务,去帮助抢舱位小队优化流程和提升他们的效率。公司的订舱需求越来越大,需求的舱位产品越来越多,而且每次只给我们几十分钟的准备时间,导致每次匆匆忙忙,人手不足,抢不到舱位则影响公司业务...

什么是Python中的生成器推导式?(生成器推导式的结果是一个)

编程派微信号:codingpy本文作者为NedBatchelder,是一名资深Python工程师,目前就职于在线教育网站Edx。文中蓝色下划线部分可“阅读原文”后点击。Python中有一种紧凑的语法...

Python技巧1:使用Python生成验证码

使用Python生成验证码

别再用手敲了,这个工具可以自动生成python爬虫代码

我们在写爬虫代码时,常常需要各种分析调试,而且每次直接用代码调试都很麻烦所以今天给大家分享一个工具,不仅能方便模拟发送各种http请求,还能轻松调试,最重要的是,可以将调试最终结果自动转换成爬虫代码,...

在 Python 中构建生成式 AI 处理器

为什么不为ApacheNiFi2.0.0创建一个Python处理器?在本教程中,了解这样做的挑战是容易还是困难。当我开始做这件事时,那是一个下雪天。我看到了IBMWatsonXPyt...

一文掌握Python生成器和迭代器之间的区别

迭代器(Iterators)迭代器是遵循迭代器协议的对象,这意味着它们实现了__iter__()和__next__()方法。__iter__()返回迭代器对象本身,__next__()返回容器中的下一...

为你的python程序上锁:软件序列号生成器

序列号很多同学可能开发了非常多的程序了,并且进行了...

5分钟掌握Python(八)之生成器(生成器 python)

1)说明:在Python中,这种一边循环一边计算的机制,称为生成器:generator。在Python中,使用了yield的函数被称为生成器(generator)。跟普通函数不同的是,生成...

python中迭代器和生成器傻傻分不清,别急,这就告诉你区别

杂谈...

使用python生成添加管理员账户的exe

0x01前言在渗透测试中,针对Windows服务器获取webshell后一般会考虑新建管理员账号(当然某些情况下可以直接读密码)登录rdp方便渗透。目前来说,常见的使用netuser(包括激活gu...

人人都能看懂的「迭代器、生成器」入门指南

来源:早起Python作者:刘早起...

用检索增强生成让大模型更强大,这里有个手把手的Python实现

选自towardsdatascience...

Markdown + 文档管理 + 静态网页生成,集大成的 Markdown 应用:MWeb

上周给大家推荐了Typora,作为一款纯粹的Markdown应用来说,它的各种功能和细节可以说已经相当极致,然而,Ulysses用户表示:我们想要的不仅仅是Markdown。是的,Markdo...

python yield -- 生成器(python 生成器send)

概念:yield和return的区别:一个是返回值,一个是迭代器,多次返回python中,yield关键字用于从一个函数中返回一个值,并且能够在之后从同一个位置继续执行。这使得yield成为...

Python生成器(Python生成器对象)

一、Python生成器介绍1.什么是生成器在Python中,使用了...