0
点赞
收藏
分享

微信扫一扫

几个SQL的高级写法

minute_5 2023-06-21 阅读 54

文章目录


前提条件

use class;
create table location (Region char(20),Store_Name char(20));
insert into location values('East','Boston');
insert into location values('East','New York');
insert into location values('West','Los Angeles');
insert into location values('West','Houston');

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M8A30HhZ-1687255797624)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620140331268.png)]

create table store_info (Store_Name char(20),Sales int(10),Date char(10));
insert into store_info values('Los Angeles','1500','2020-12-05');
insert into store_info values('Houston','250','2020-12-07');
insert into store_info values('Los Angeles','300','2020-12-08');
insert into store_info values('Boston','700','2020-12-08');

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-76Uc2W4D-1687255797625)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620140459620.png)]


一、常用查询

1. SELECT(显示查询)

SELECT "字段" FROM "表名";

例如
select Store_name from store_info;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-swNPEhYH-1687255797625)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620140533955.png)]

2. DISTINCT(不重复查询)

SELECT DISTINCT "字段" FROM "表名";

例如
select distinct Store_Name from store_info;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TB8ttezc-1687255797626)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620140550840.png)]

3. WHERE(有条件查询)

SELECT "字段" FROM "表名" WHERE "条件";

例如
select distinct Store_Name from store_info where Sales > 1000;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MDvqQTZW-1687255797626)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620141321876.png)]

4. AND/OR(且/或)

SELECT "字段" FROM "表名" WHERE "条件1" {[AND|OR] "条件2"} + ;

例如
select Store_Name from store_info where Sales > 1000 OR ( Sales < 500 and Sales > 200 );

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-61Kt25MA-1687255797627)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620141404552.png)]

5. IN (显示已知值的字段)

SELECT "字段" FROM "表名" WHERE "字段" IN ('值1', '值2', ...);

例如
select * from store_info where Store_Name in ('Los Angeles', 'Houston');

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2JnfgOtj-1687255797627)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620141429181.png)]
另外还有not in命令,用法一致,表示显示不在指定范围内的字段的值。

6. BETWEEN(显示两个值范围内的字段)

SELECT "字段" FROM "表名" WHERE "字段"  BETWEEN '值1' AND '值2';

例如
select * from store_info where Date between '2020-12-06' and '2020-12-10';

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iOdqgdiw-1687255797628)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620141538394.png)]

7. 通配符的使用

'A_Z':所有以 'A' 起头,另一个任何值的字符,且以 'Z' 为结尾的字符串。例如,'ABZ''A2Z' 都符合这一个模式,而 'AKKZ' 并不符合 (因为在 A 和 Z 之间有两个字符,而不是一个字符)'ABC%': 所有以 'ABC' 起头的字符串。例如,'ABCD''ABCABC' 都符合这个模式。
'%XYZ': 所有以 'XYZ' 结尾的字符串。例如,'WXYZ''ZZXYZ' 都符合这个模式。
'%AN%': 所有含有 'AN'这个模式的字符串。例如,'LOS ANGELES''SAN FRANCISCO' 都符合这个模式。
'_AN%':所有第二个字母为 'A' 和第三个字母为 'N' 的字符串。例如,'SAN FRANCISCO' 符合这个模式,而 'LOS ANGELES' 则不符合这个模式。

LIKE (匹配关键字查询)

SELECT "字段" FROM "表名" WHERE "字段" LIKE {模式};

例如
select * from store_info where Store_Name like '%os%';

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nqItwmTs-1687255797628)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620141620857.png)]

8. ORDER BY (按关键字排序)

SELECT "字段" FROM "表名" [WHERE “条件”] ORDER BY "字段" [ASC, DESC];
#ASC 	是按照升序进行排序的,是默认的排序方式。
#DESC 	是按降序方式进行排序。

例如
select Store_Name,Sales,Date from store_info order by Sales DESC;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AnAOrwut-1687255797628)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620141645511.png)]

二、常用函数

1. 数学函数

关键字含义
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…)返回集合中最小的值,也可以返回多个字段的最小的值
select abs(-1), rand(), mod(5,3), power(2,3), round(1.89);

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6cesclfZ-1687255797629)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620141747900.png)]

select round(1.8937,3), truncate(1.235,2), ceil(5.2), floor(2.1), least(1.89,3,6.1,2.1);

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-V04tfNVS-1687255797629)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620141808995.png)]

#求一组数据最大值
create table t1 (id1 int, id2 int, id3 int, id4 int);
insert into t1 values (10, 50, 30, 20);
select * from t1;
select greatest(id1,id2,id3,id4) from t1;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9m8oGZmf-1687255797630)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620142853498.png)]

#求1000以内的随机整数
select round(rand() * 1000);
select truncate(rand() * 1000,0);

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dcxEJv40-1687255797630)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620142922318.png)]

2. 聚合函数

关键字含义
avg()返回指定列的平均值
count()返回指定列中非 NULL 值的个数
min()返回指定列的最小值
max()返回指定列的最大值
sum(x)返回指定列的所有值之和
#求平均值
select avg(Sales) from store_info;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aft7VI8S-1687255797630)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620142952378.png)]

#求表中城市数量
select count(Store_Name) from store_info where Store_Name is NOT NULL;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uV07qn1a-1687255797631)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620143001063.png)]

#统计数量,进行去重
select count(DISTINCT Store_Name) from store_info;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uQFEyunh-1687255797631)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620143007782.png)]

#统计字段中最大值
select max(Sales) from store_info;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ooZPNcCv-1687255797631)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620143050512.png)]

#统计字段中最小值
select min(Sales) from store_info;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-enURmoPY-1687255797631)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620143058997.png)]

#统计字段中的总和
select sum(Sales) from store_info;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Q5rRmw9P-1687255797632)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620143104492.png)]

3. 字符串函数

关键字含义
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 反转
#输出指定一个字段中某一部分
#命令行
i='123456789'
echo ${i:3:4}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jLr7i60P-1687255797632)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620145401175.png)]

#字段替换
#命令行操作
i='123456789'
echo $i | tr '456' 'abc'

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6lMes6ye-1687255797632)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620145306764.png)]

#大小写字母替换
#命令行操作
echo abc123xyz | tr 'a-z' 'A-Z'

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EfDv9jRu-1687255797633)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620145349286.png)]

#字段拼接
select concat(Region, Store_Name) from location where Store_Name = 'Boston';

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-n4yj5WC3-1687255797633)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620143346203.png)]

#如sql_mode开启开启了PIPES_AS_CONCAT,"||"视为字符串的连接操作符而非或运算符,
#和字符串的拼接函数Concat相类似,这和Oracle数据库使用方法一样的
select Region || ' ' || Store_Name from location where Store_Name = 'Boston';

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-izpkQijY-1687255797634)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620143450953.png)]

select substr(Store_Name,5) from location where Store_Name = 'Los Angeles';
select substr(Store_Name,3,4) from location where Store_Name = 'New York';

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CMzpF4bD-1687255797634)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620143659443.png)]

SELECT TRIM ([ [位置] [要移除的字符串] FROM ] 字符串);
#[位置]:的值可以为 LEADING (起头), TRAILING (结尾), BOTH (起头及结尾)。 
#[要移除的字符串]:从字串的起头、结尾,或起头及结尾移除的字符串。缺省时为空格。

例如
select trim(leading 'New ' from 'New York');
select trim(leading 'Los' from (select store_name from location where store_name = 'Los Angeles'));

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9xevhfWr-1687255797634)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620144436274.png)]

select replace(Region, 'ast', 'astern')from location;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cHBnmPll-1687255797635)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620143841232.png)]

三、查询与函数

1. GROUP BY(汇总分组)

SELECT "字段1", SUM("字段2") FROM "表名" GROUP BY "字段1";

例如
#对分组的总和进行降序排序
select Store_Name,sum(Sales) from store_info group by Store_Name order by sales desc;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QgzP10x4-1687255797635)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620144452549.png)]

2. HAVING (过滤返回值)

SELECT "字段1", SUM("字段2") FROM "表格名" GROUP BY "字段1" HAVING (函数条件);

例如
select Store_Name, SUM(Sales) from store_info group by Store_Name having SUM(Sales) > 1500;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-laBozDPj-1687255797636)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620144613694.png)]

3. 别名(字段别名 表格别名)

SELECT "表格別名"."字段1" [AS] "字段別名" FROM "表格名" [AS] "表格別名";

例如
select A.Store_Name Store, SUM(A.Sales) "Total Sales" from store_info A group by A.Store_Name;
select Store_Name,sum(Sales) as "Total Sales" from store_info  group by Store_Name;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yQefuTDH-1687255797636)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620144639200.png)]

4. 子查询

SELECT "字段1" FROM "表格1" WHERE "字段2" [比较运算符] 				#外查询
(SELECT "字段1" FROM "表格2" WHERE "条件");						   #内查询
#内查询优先级高于外查询

#可以是符号的运算符,例如 =、>、<、>=、<= ;也可以是文字的运算符,例如 LIKE、IN、BETWEEN
例如
#查询两个表交集的城市
select * from location where store_name in (select store_name from store_info);

#查询在location表中西部城市的销售总和
select sum(sales) from store_info where store_name in (select store_name from location where region = 'West');

#通过多表查询,查询两个表中有交集的值
select sum(A.Sales) from store_info A where A.Store_Name in (select Store_Name from location B where B.Store_Name = A.Store_Name);

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Cp4Sh4fA-1687255797636)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620144740036.png)]

5. EXISTS(类查询)

SELECT "字段1" FROM "表格1" WHERE EXISTS (SELECT * FROM "表格2" WHERE "条件");
例如
#判断是否有西部城市,有的话就计算销售和
,如果没有就输出NULL
select SUM(Sales) from store_info where exists (select * from location where Region = 'West');

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uIGixYUA-1687255797637)(C:\Users\86138\AppData\Roaming\Typora\typora-user-images\image-20230620144806324.png)]

总结

1. sql 语句中 HAVING 的用法

having 对 group by 分组后的结果根据条件进行过滤筛选。

举报

相关推荐

0 条评论