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

[mysql8循序渐进]八 查询DQL

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

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

相关推荐

SQL轻松入门(5):窗口函数(sql语录中加窗口函数的执行)

01前言标题中有2个字让我在初次接触窗口函数时,真真切切明白了何谓”高级”?说来也是一番辛酸史!话说,我见识了窗口函数的强大后,便磨拳擦掌的要试验一番,结果在查询中输入语句,返回的结果却是报错,Wh...

28个SQL常用的DeepSeek提示词指令,码住直接套用

自从DeepSeek出现后,极大地提升了大家平时的工作效率,特别是对于一些想从事数据行业的小白,只需要掌握DeepSeek的提问技巧,SQL相关的问题也不再是个门槛。...

从零开始学SQL进阶,数据分析师必备SQL取数技巧,建议收藏

上一节给大家讲到SQL取数的一些基本内容,包含SQL简单查询与高级查询,需要复习相关知识的同学可以跳转至上一节,本节给大家讲解SQL的进阶应用,在实际过程中用途比较多的子查询与窗口函数,下面一起学习。...

SQL_OVER语法(sql语句over什么含义)

OVER的定义OVER用于为行定义一个窗口,它对一组值进行操作,不需要使用GROUPBY子句对数据进行分组,能够在同一行中同时返回基础行的列和聚合列。...

SQL窗口函数知多少?(sql窗口怎么执行)

我们在日常工作中是否经常会遇到需要排名的情况,比如:每个部门按业绩来排名,每人按绩效排名,对部门销售业绩前N名的进行奖励等。面对这类需求,我们就需要使用sql的高级功能——窗口函数。...

如何学习并掌握 SQL 数据库基础:从零散查表到高效数据提取

无论是职场数据分析、产品运营,还是做副业项目,掌握SQL(StructuredQueryLanguage)意味着你能直接从数据库中提取、分析、整合数据,而不再依赖他人拉数,节省大量沟通成本,让你...

SQL窗口函数(sql窗口函数执行顺序)

背景在数据分析中,经常会遇到按某某条件来排名、并找出排名的前几名,用日常SQL的GROUPBY,ORDERBY来实现特别的麻烦,有时甚至实现不了,这个时候SQL窗口函数就能发挥巨大作用了,窗...

sqlserver删除重复数据只保留一条,使用ROW_NUMER()与Partition By

1.使用场景:公司的小程序需要实现一个功能:在原有小程序上,有一个优惠券活动表。存储着活动产品数据,但因为之前没有做约束,导致数据的不唯一,这会使打开产品详情页时,可能会出现随机显示任意活动问题。...

SQL面试经典问题(一)(sql经典面试题及答案)

以下是三个精心挑选的经典SQL面试问题及其详细解决方案,涵盖了数据分析、排序限制和数据清理等常见场景。这些问题旨在考察SQL的核心技能,适用于初学者到高级开发者的面试准备。每个问题均包含清晰的...

SQL:求连续N天的登陆人员之通用解答

前几天发了一个微头条:...

SQL四大排序函数神技(sql中的排序是什么语句)

在日常SQL开发中,排序操作无处不在。当大家需要排序时,是否只会想到ORDERBY?今天,我们就来揭秘SQL中四个强大却常被忽略的排序函数:ROW_NUMBER()、RANK()、DENSE_RAN...

四、mysql窗口函数之row_number()函数的使用

1、窗口函数之row_number()使用背景窗口函数中,排序函数rank(),dense_rank()虽说都是排序函数,但是各有用处,假如像上章节说的“同组同分”两条数据,我们不想“班级名次”出现“...

ROW_NUMBER()函数(rownumber函数与rank区别)

ROW_NUMBER()是SQL中的一个窗口函数(WindowFunction)...

Dify「模板转换」节点终极指南:动态文本生成进阶技巧(附代码)Jinja2引擎解析

这篇文章是关于Dify「模板转换」节点的终极指南,解析了基于Jinja2模板引擎的动态文本生成技巧,涵盖多源文本整合、知识检索结构化、动态API构建及个性化内容生成等六大应用场景,助力开发者高效利用模...

Python 最常用的语句、函数有哪些?

1.#coding=utf-8①代码中有中文字符,最好在代码前面加#coding=utf-8②pycharm不加可能不会报错,但是代码最终是会放到服务器上,放到服务器上的时候运行可能会报错。③...