Loading

一、SQL高级语句


摘抄别的博主的博客主要总去CSDN看不太方便自己整理一下加深记忆!

导入文件至数据库

#将脚本导入  source  加文件路径
mysql> source /backup/test.sql;

![img]( https://img-blog.csdnimg.cn/4bab383032f9495ba8d9f241e58c1613.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_19,color_FFFFFF,t_70,g_se,x_16)

select

显示表格中的一个或者多个字段中所有的信息

#语法:
select 字段名  from 表名;
示例1:
select * from students;

示例2:
select * from students;

![img]( https://img-blog.csdnimg.cn/4a6b608ed6c34b81802c65ee2bd9b700.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_18,color_FFFFFF,t_70,g_se,x_16)

distinct

查询不重复记录

#语法:
select distinct 字段 from 表名﹔
#示例1:去除年龄字段中重复的
select distinct age from students;

![img]( https://img-blog.csdnimg.cn/2625d517438144b5a0196152a00e2447.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例2:查找性别
select distinct gender from students;

![img]( https://img-blog.csdnimg.cn/3ec8dc549804485a8a2baf049aba102e.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

where

where 有条件的查询

#语法:
select '字段' from 表名  where 条件
#示例:显示name和age 并且要找到age小于20
select name,age from students where age < 20;

#示例:显示name和age 并且要找到age小于20
select name,age from students where age < 20;

![img]( https://img-blog.csdnimg.cn/ad4c2392505741eea61e46a83aec79f6.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

and;or

and 且 ; or 或

#语法:
select 字段名  from 表名 where 条件1 (and|or) 条件2 (and|or)条件3;

示例1:显示name和age 并且要找到age大于20小于30
select name,age from students where age >20 and age <30;

![img]( https://img-blog.csdnimg.cn/b74aa69ff423466ab03cea0c15007d83.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

in

显示已知值的资料

#语法:
select 字段名  from 表名 where 字段 in ('值1','值2'....);
#示例1:显示学号为1,2,3,4的学生记录
select * from students where StuID in (1,2,3,4);

![img]( https://img-blog.csdnimg.cn/4fd8861bb77846f99008ee509a05bb47.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例2:显示班级为1和3的学生记录
select * from students where ClassID in (1,3);

![img]( https://img-blog.csdnimg.cn/594e9eaff5884863b8cd3a172281504c.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

between

显示两个值范围内的资料

#语法:
select 字段名  from 表名 where 字段 between  '值1' and '值2';
包括 and两边的值
#示例1:显示学生姓名在Ding Dian和Hua Rong中的学生记录
select * from students where name between 'ding dian' and 'Hua Rong';

![img]( https://img-blog.csdnimg.cn/958f5a8f9d144f0c838b91081a5579b1.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例2:显示学生号码id在2-5 的信息
select * from students where stuid between 2 and 5;

![img]( https://img-blog.csdnimg.cn/de690c6873e1452885670dc354b70fa2.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例3:显示学生年龄在20-35之间的信息,不需要表中一定有该字段,只会将20到25 已有的都显示出来
select * from students where age between 20 and 25;

![img]( https://img-blog.csdnimg.cn/5e1bf5c1c77f425197c9daa5aa2233c4.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

like 通配符

通配符通常是和 like 一起使用

#语法:
select 字段名  from 表名 where 字段 like 模式
通配符 含义
% 表示零个,一个或者多个字符
_ 下划线表示单个字符
A_Z 所有以A开头 Z 结尾的字符串 'ABZ' 'ACZ' 'ACCCCZ'不在范围内 下划线只表示一个字符 AZ 包含a空格z
ABC% 所有以ABC开头的字符串 ABCD ABCABC
%CBA 所有以CBA结尾的字符串 WCBA CBACBA
%AN% 所有包含AN的字符串 los angeles
_AN% 所有 第二个字母为 A 第三个字母 为N 的字符串
#示例1:查找名字以s开头的学生记录
select * from students where name like 's%';

![img]( https://img-blog.csdnimg.cn/fb44d9ff1624409d935cbafc7fdc7d5c.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例2:查找名字包含ong的学生记录
select * from students where name like '%ong%';

![img]( https://img-blog.csdnimg.cn/68ec4ea66cba47fba09e38399d936fc1.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例3:查找名字第二个字母为u,第三个字母为a的学生记录
select * from students where name like '_ua%';

![img]( https://img-blog.csdnimg.cn/7a3be1211f604b46b6094a1cdb81d463.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

order by

order by 按关键字排序

#语法:
select 字段名  from 表名 where 条件 order by 字段 [asc,desc];
asc :正向排序
desc :反向排序
默认是正向排序
#示例1:按学生的年龄正向排序显示年龄和姓名字段
select age,name from students order by age;

![img]( https://img-blog.csdnimg.cn/e86233092a494834b786974a2958c587.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例2:按学生的年龄反向排序显示年龄和姓名字段
select age,name from students order by age desc;

![img]( https://img-blog.csdnimg.cn/7b6ca45674f448bd86233da196394a77.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例3:显示name、age和classid字段的数据 并且只显示classid字段为3 的 并且以age字段排序
select age,name,classid from students where classid=3 order by age;

![img]( https://img-blog.csdnimg.cn/baded73d9c934e8c8c969b671c965276.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

函数

数学函数


函数 含义
abs(x) 返回x 的 绝对值
rand() 返回0到1的随机数
mod(x,y) 返回x除以y以后的余数
power(x,y) 返回x的y次方
round(x) 返回离x最近的整数
round(x,y) 保留x的y位小数四舍五入后的值
sqrt(x) 返回x的平方根
truncate(x,y) 返回数字 x 截断为 y 位小数的值
ceil(x) 返回大于或等于 x 的最小整数
floor(x) 返回小于或等于 x 的最大整数
greatest(x1,x2.....) 返回返回集合中最大的值
least(x1,x2..........) 返回返回集合中最小的值
#示例1:返回-2的绝对值
select abs(-2);

![img]( https://img-blog.csdnimg.cn/59f8d92625a94f19a2e8fe5d9e7d392f.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_19,color_FFFFFF,t_70,g_se,x_16)

#示例2:随机生成一个数
select rand (1);

![img]( https://img-blog.csdnimg.cn/3662b4483f3040739c2d3259c51b809d.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_19,color_FFFFFF,t_70,g_se,x_16)

#示例3:随机生成排序
select * from students order by rand();

![img]( https://img-blog.csdnimg.cn/a74d5d8031b243288464249570fd91dd.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

![img]( https://img-blog.csdnimg.cn/09a69b6a888f4eb39248a62ce70ff103.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例4:返回7除以2以后的余数
select mod(7,2);

![img]( https://img-blog.csdnimg.cn/2a396a9622ec4077a55362a73622559e.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_17,color_FFFFFF,t_70,g_se,x_16)

#示例5:返回2的3次方
select power(2,3);

![img]( https://img-blog.csdnimg.cn/620ad2ca8364436f97add1b42d38db33.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例6:返回离2.6最近的数
select round(2.6);
#返回离2.4最近的数

![img]( https://img-blog.csdnimg.cn/6e3da2bfe8564e00b821ba9f7a73fa85.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

![img]( https://img-blog.csdnimg.cn/6f86909ccb464a11a78db7e2eeeb0524.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例7:保留2.335321的3位小数四舍五入后的值
select round(2.335321,2);

![img]( https://img-blog.csdnimg.cn/52c1fcb941e84bc49aef34efa2641ee8.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例8:返回数字 2.335321 截断为2位小数的值
select truncate(2.335321,2);

![img]( https://img-blog.csdnimg.cn/b2d3a31f12bc417fbacbc616032882fe.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例9:返回大于或等于2.335321 的最小整数
select ceil(2.335321);

![img]( https://img-blog.csdnimg.cn/ca85df88c3304d7b9eb8a625d459119a.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例10:返回小于或等于 2.335321 的最大整数
select floor(2.335321);

![img]( https://img-blog.csdnimg.cn/1eb923c9204a42da9f1fd249520b0ad8.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例11:返回集合中最大的值
 select greatest(1,4,3,9,20);

![img]( https://img-blog.csdnimg.cn/c502f6eab3f14b3684b42318ce1dd78c.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例12:返回集合中最小的值
select least(1,4,3,9,20);

![img]( https://img-blog.csdnimg.cn/514a2581ede549a49866ff85c7dde82e.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

聚合函数


函数 含义
avg() 返回指定列的平均值
count() 返回指定列中非 NULL 值的个数
min() 返回指定列的最小值
max() 返回指定列的最大值
sum(x) 返回指定列的所有值之和
#示例1:求表中年龄的平均值
select avg(age) from students;

![img]( https://img-blog.csdnimg.cn/d7d88f1de491475980cbb710525f50c5.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例2:求表中年龄的总和
select sum(age) from students;

![img]( https://img-blog.csdnimg.cn/943d577c4e8e446ba0a2b927f680f19a.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例3:求表中年龄的最大值
select max(age) from students;

![img]( https://img-blog.csdnimg.cn/eaa222a530ab43f5993ac54910125423.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例4:求表中年龄的最小值
select min(age) from students;

![img]( https://img-blog.csdnimg.cn/9db8fd7727234397a23bfa0192041acf.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例5:求表中有多少班级字段非空记录
select count(classid) from students;

count(明确字段):不会忽略空记录

![img]( https://img-blog.csdnimg.cn/c47e150629394439a5002cd3e01b6f9a.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

![img]( https://img-blog.csdnimg.cn/f0259f4af8764e17aa865f285d670dc6.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例6:求表中有多少条记录
select count(*) from students;

count(*)包含空字段,会忽略空记录

![img]( https://img-blog.csdnimg.cn/f76a46d0fa5942adb8f38eeea689d249.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例7:看空格字段是否会被匹配
insert into students values(26,' ',28,'f',1,8);

![img]( https://img-blog.csdnimg.cn/e6f6dd7b5e0c41b389b23db352edc9a6.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

字符串函数


函数 描述
trim() 返回去除指定格式的值
concat(x,y) 将提供的参数 x 和 y 拼接成一个字符串
substr(x,y) 获取从字符串 x 中的第 y 个位置开始的字符串,跟substring()函数作用相同
substr(x,y,z) 获取从字符串 x 中的第 y 个位置开始长度为z 的字符串
length(x) 返回字符串 x 的长度
replace(x,y,z) 将字符串 z 替代字符串 x 中的字符串 y
upper(x) 将字符串 x 的所有字母变成大写字母
lower(x) 将字符串 x 的所有字母变成小写字母
left(x,y) 返回字符串 x 的前 y 个字符
right(x,y) 返回字符串 x 的后 y 个字符
repeat(x,y) 将字符串 x 重复 y 次
space(x) 返回 x 个空格
strcmp(x,y) 比较 x 和 y,返回的值可以为-1,0,1
reverse(x) 将字符串 x 反转

1)trim

语法:
select trim (位置 要移除的字符串 from 字符串)
其中位置的值可以是 
leading(开始) 
trailing(结尾)
both(起头及结尾)

#区分大小写
要移除的字符串:从字符串的起头、结尾或起头及结尾移除的字符串,缺省时为空格。
#示例1:从名字开头的开始,移除Sun Dasheng中的Sun显示
select trim(leading 'Sun' from 'Sun Dasheng');

![img]( https://img-blog.csdnimg.cn/000537fd84f54f5db5004312f59b4580.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例2:去除空格
select trim(both  from ' zhang san      ');

![img]( https://img-blog.csdnimg.cn/46a68d0ad56549c5ae6993c391f90e39.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

2)length

#语法:
select length(字段) from 表名;
#示例:计算出字段中记录的字符长度
select name,length(name) from students;

![img]( https://img-blog.csdnimg.cn/d7e2748382cf48fe8bb7ac6659077f15.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

3)replace

#语法:
select replace(字段,'原字符''替换字符') from 表名;
select replace(name,'ua','hh') from students;

![img]( https://img-blog.csdnimg.cn/967b6f72fca2455a9c95cb6ec28c4edc.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

![img]( https://img-blog.csdnimg.cn/1dcc877f6b4f4687b6e0aac4f08c3adc.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

4)concat

#语法:
select concat(字段1,字段2)from 表名
#示例1:将name,classid字段拼接成一个字符串
select concat(name,classid) from students;

![img]( https://img-blog.csdnimg.cn/a6b0041e784a4fe0a533a2805ab69583.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例2:只显示3ban的名字和classid为一个组合记录
select concat(name,classid) from students where classid=3;

![img]( https://img-blog.csdnimg.cn/7a4b25233ec44829854d04f246173cc2.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例3:中间加制表符
select concat(name,'\t',classid) from students where classid=3;

![img]( https://img-blog.csdnimg.cn/06ba8e4e0e0c4e78b1010944477efcf4.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

4)substr

#语法:
select substr(字段,开始截取字符,截取的长度)  where 字段='截取的字符串' 
#示例1:截取第6个字符往后
select substr(name,6) from students where name='Yue Lingshan';

![img]( https://img-blog.csdnimg.cn/11482b28263d474083f0721a1cf793e8.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例2:截取第6个字符往后的两个字符
select substr(name,6,2) from students where name='Yue Lingshan';

![img]( https://img-blog.csdnimg.cn/542e1c5dd1b7442485fe475f91765a8c.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

group by

对group by 后面的字段的查询结果进行汇总分组,通常是结合聚合函数一起使用的

group by 有一个原则,就是select 后面的所有列中,没有使用聚合函数的列必须出现在 group by 的后面。

#语法:
select 字段1,sum(字段2) from 表名 group by 字段1;
#示例1:求各个班的年龄总和
select classid,sum(age) from students group by classid;

![img]( https://img-blog.csdnimg.cn/599a971556594f8685cdc770c1f67787.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例2:求各个班的平均年龄
select classid,avg(age) from students group by classid;

![img]( https://img-blog.csdnimg.cn/3594016ff43244a183fd59bf021a3521.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例3:根据年龄查看每个班的人数
select classid,count(age) from students group by classid;

![img]( https://img-blog.csdnimg.cn/1ae944c00b234d54b49053626f3776e2.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

having

having:用来过滤由group by语句返回的记录集,通常与group by语句联合使用

having语句的存在弥补了where关键字不能与聚合函数联合使用的不足。如果被SELECT的只有函数栏,那就不需要GROUP BY子句。

要根据新表中的字段,来指定条件

#语法:
SELECT 字段1,SUM("字段")FROM 表格名 GROUP BY 字段1 having(函数条件);
#示例:查看各个班的平均年龄在30以上的班级
select classid,avg(age) from students group by classid having avg(age) > 30;

![img]( https://img-blog.csdnimg.cn/5d9279e7670446529a4747c59ea29b4e.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

![img]( https://img-blog.csdnimg.cn/404b77a72075453cb82313e7081ba095.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

别名

栏位別名 表格別名

v#语法:
SELECT "表格別名"."栏位1" [AS] "栏位別名" FROM "表格名" [AS] "表格別名";
#示例:设置表名别名为f,基于班级号来统计各班年龄总和,sum(age)定义别名为total age
select f.classid,sum(age) 'total age' from students as f group by f.classid;

![img]( https://img-blog.csdnimg.cn/b48564d7b1ea4f72a3f5977be58e7d33.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

连接查询

1)inner join(等值相连)

只返回两个表中联结字段相等的行

SELECT * FROM students A INNER JOIN scores B on A.stuid = B.stuid;

![img]( https://img-blog.csdnimg.cn/fcf7a42efa354d248b92d5bd0af10333.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

2)left join(左联接)

返回包括左表中的所有记录和右表中联结字段相等的记录

select * from scores A left join students B on A.stuid = B.stuid;

![img]( https://img-blog.csdnimg.cn/de684702fe6646a5b8319f69cf37a09f.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

![img]( https://img-blog.csdnimg.cn/f8d73719f4574b1aa0c46a6027cbbe9d.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

3)right join(右联接)

select * from scores A right join students B on A.stuid = B.stuid;

![img]( https://img-blog.csdnimg.cn/0bd701a4cf6645c48b2349bcca141e24.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

子查询

连接表格,在WHERE 子句或HAVING 子句中插入另一个SQL语句

语法:
SELECT "栏位1" FROM "表格1" WHERE "栏位2"	[比较运算符]			
#外查询
(SELECT "栏位1" FROM "表格1" WHERE "条件");	
#示例:查询学生学号为1的得分总和
select sum(score) from scores where stuid in (select stuid from students where stuid=1);

![img]( https://img-blog.csdnimg.cn/a0a8dffb97a646aaad04754d8570e2a4.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

EXISTS

用来测试内查询有没有产生任何结果类似布尔值是否为真

如果有的话,系统就会执行外查询中的SQL语句。若是没有的话,那整个 SQL 语句就不会产生任何结果。

#语法:
SELECT "栏位1" FROM "表格1" WHERE EXISTS (SELECT * FROM "表格2" WHERE "条件");
#示例1:先看students表中是否有stuid为1的学生,如果有则执行将scores表中的score求和
select sum(score) from scores  where exists (select * from students where stuid=1);

![img]( https://img-blog.csdnimg.cn/3a3f43989541477388d25f77f5fc58ad.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

![img]( https://img-blog.csdnimg.cn/ea564fb12e914017a702630c7915b067.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

#示例2:先看students表中是否有stuid为88的学生,如果有则执行将scores表中的score求和
select sum(score) from scores  where exists (select * from students where stuid=88);

![img]( https://img-blog.csdnimg.cn/523cc374e28049a399c8e7602bd95c87.png?x-oss-process=image/watermark ,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5bCP6ICD5ouJMTIz,size_20,color_FFFFFF,t_70,g_se,x_16)

posted @ 2021-12-27 16:49  秃头版胡歌  阅读(843)  评论(0)    收藏  举报