【SQL】打算开一个新坑玩
前言:
之前工作中浅接触了一下postgresql数据库,用pgAdmin终端和python库的cursor获取过数据,做过一些基础的数据分析,语句基本都是现查。
感觉如果系统性地学习一下sql可能也对数据索取效率提高有帮助。
2025.12.20
基础语法:
select a * b as a_b
select distinct a, b
where > < = or and
is null / is not null
like "%xx%", like "_y"
order by xx asc, yy desc
limit [start], [offset]
CASE WHEN (条件1) THEN 结果1 WHEN (条件2) THEN 结果2 …… ELSE 其他结果 END as xxx
函数:
时间函数,有很多,如date(), time(), datetime()
字符串函数,upper(), lower(), length()
聚合函数(用于搭配group by), sum(), count(), max(), min(), avg()
分组聚合-单字段分组,select a, avg(b) as avg_b from xx group by a
分组聚合-多字段分组,select a, avg(b) as avg_b from xx group by a, c
分组聚合-having子句,在分组之后用于筛选
查询进阶-关联查询 cross join, inner join, outer join(left join, right join)
查询进阶-子查询 where xx in (select ...), where [not] exists (select ...)
查询进阶-组合查询 union, union all
查询进阶-开窗函数 sum(x) over (partition by y), sum(x) over (partition by y order by z asc)
posted on 2025-12-20 19:56 Mju_halcyon 阅读(0) 评论(0) 收藏 举报
浙公网安备 33010602011771号