点击上方SQL数据库开发,关注获取SQL视频教程
SQL专栏
上周给大家分享了一部分《SQL基础教程》学习笔记,今天将剩下部分继续分享给大家。
5 复杂查询
5.1 视图
--创建视图
create view <视图名称>
as
<select语句>;
--视图可以使用在select语句的from子句中,并且视图会自动更新
select子句中未使用distinct
from 子句中只有一张表
未使用group by子句
未使用having子句
drop view <视图名称>;
子查询就是一次性视图(select语句),与视图不同,子查询在select语句执行完毕就会消失
首先执行内层查询,再执行外层查询
子查询必须设定名称,尽量从处理内容的角度出发为子查询设定恰当的名称,使用as关键字,也可省略
select product_type,product_name,sale_price
from Product as P1
where sale_price >
(select avg(sale_price)
from Product as P2
where P1.product_type = P2.product_type
--结合条件写在子查询中
group by product_type
);
--算术函数除了常见的四则运算(+-x/),介绍常见一些函数,numeric是大多数DBMS都支持的一种数据类型,参数是null,返回值也是null
select round(1123.26723,2);
--结果:1123.27 四舍五入
select abs(-1)
--结果:1 绝对值
select mod(7,3)
--结果:1 取余
concat() --拼接字符串
length() --在sql server中无法使用,sql server中使用len()函数,汉子占两个字符,英文字母占一个
--在MySQL中有length()和char_length()
lower() --只对英文字母使用,将参数中的字符串全部转化成小写字母,upper() --将小写转化成大写
replace(对象字符串,替换前的字符串,替换后的字符串)
substring(对象字符串 from 截取的位置 for 截取的字符个数) --可以截取字符串一部分
current_date--当前日期
current_time--当前时间
current_timestamp--挡墙的日期和时间
extract()函数--截取日期函数,返回值并不是日期型,而是数值类型
cast函数--类型转换
cast(转换前的值 as 想要转换的数据类型)
select cast("ooo1" as interger)
coalesce函数 --将null转换为其他值
--coalesce是sql特有的函数,返回可变参数中左侧第一个不是null的值
coalesce(数据1,数据2,数据3...)
like --字符串的部分一致查询
-- 前方一致
select * from SampleLike where strcol like "ddd%"; --代表"0个字符以上的字符串"
-- 中间一致
select * from SampleLike where strcol like "%ddd%"; --代表"0个字符以上的字符串"
-- 后方一致
select * from SampleLike where strcol like "%ddd";
--使用%和_(下划线)进行后方一致查询
select * from SampleLike where strcol like "ddd_ _"; --进行ddd+任意两个字符
between --范围查询
is null 、is not null --判断是否是null
in --or的谓词
exists
case when <求值表达式> then <表达式> -- <求值表达式> 类似于"列=值"
when <求值表达式> then <表达式>
when <求值表达式> then <表达式>
.
.
else <表达式> --指定了不满足when子句中的条件的记录,null之外其他值或表达式都可写在else中
end --不能省略
select product_name,
case when product_type = "衣服"
then "A":'|| product_type'
when product_type = "办公用品"
then "B":'|| product_type'
when product_type = "厨房用品"
then "C":'|| product_type'
else null
end as abc_product_type
from Product;
select product_name,
case product_type --写过一次,不用再写
when "衣服" then "A":'|| product_type'
when "办公用品" then "B":'|| product_type'
when "厨房用品" then "C":'|| product_type'
end as abc_product_type
from Product;
select product_name,product_price
from Product
where purchase_price not in (500,2800,5000);
select product_name,product_price
from Product
where purchase_price not in (500,2800,5000,null);
--结果是什么都没有,使用子查询作为not in 的参数时,子查询的返回值也不能是null
select product_id,sale_price from Product
union
select product_id,sale_price from Product2;
select product_id,sale_price from Product
union all
select product_id,sale_price from Product2;
select product_id,sale_price from Product
intersect
select product_id,sale_price from Product2
order by product_id;
select product_id,sale_price from Product
except
select product_id,sale_price from Product2
order by product_id;
<窗口函数> over ([partition by <列清单>]
order by <排序用列清单>)
--能够作为窗口函数的函数
--聚合函数(sum,avg,count,max,min)
--rank,dense_rank,row_number等专用窗口函数
select product_name,product_type,sale_price,
rank() over (partition by product_type
order by sale_price) as ranking
from Product;
select product_id,product_name,sale_price,
avg(sale_price) over (order by product_id
rows 2 preceding) as moving_avg
from Product;
select product_type,sum(sale_price) as sum_price
from Product
group by rollup(product_type);
--使用了rollup时多出了最上方的合计和个分类的小计
select grouping(product_type) as product_type,
grouping(regist_date) as regist_date,
sum(sale_price) as sum_price
from Product
group by rollup(product_type,regist_date);
关注SQL数据库开发公众号,在后台回复关键字:资料领取,可以获取一份精心整理的技术干货。
推荐阅读

点击「阅读原文」了解SQL训练营
文章转载自SQL数据库开发,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




