PostgreSql常用SQL大全

DDL库/表结构(建库,建表,改字段,删表)

plsql 复制代码
# 创建数据库
create database test_db

# 建表
create table user(
  id serial primary key,
  name varchar(32) not null,
  age int null
)

# 修改表字段
alter table user add column email varchar(50) # 新增字段
alter table user drop column email # 删除字段
alter table user alter column name type varchar(50) # 修改字段类型

# 删除表
drop table user

DML增删查改

plsql 复制代码
# 新增 insert
insert into user (name,age) values ('张三',22)

insert into user (name,age) values ('张三',22),('李四',22)

# 修改 update
update user set age=23 where id=1

update user set age=23 where id in (1,2,3)

# 删除 delete
delete from user where id=1

truncate table user # 清空全表
plsql 复制代码
# 查询 select
select * from user # 全查
select id,name from user # 指定字段
select * from user where id=1 # 等于
select * from user where age between 20 and 30 # 区间 between and
select * from user where id in (1,2,3) # in多选
select * from user where age is null # 空
select * from user where age is not null # 非空
select * from user where age>20 and age<40
select * from user where age=20 or age=40

# 模糊匹配 like 区分大小写
select * from user where name like '张%' 
select * from user where name like '%张%'
select * from user where name like '%张'

# 模糊匹配 ilike 不区分大小写
select * from user where name ilike '%zhang%'

# 排序 order by
select * from user order by age asc # 升序
select * from user order by age desc # 降序

# 分页 limit offset
select * from user limit 3 offset 0 # 前3条
select * from user limit 3 offset 4 # 第 5~7 条

联表查询

plsql 复制代码
# 内连接 inner join
select user.id,user.name,todos.title,todos.is_finish from user inner join todos on user.id = todos.id

# 左连接 left join
select user.id,user.name,todos.title,todos.is_finish from user left join todos on user.id = todos.id

聚合+分组

plsql 复制代码
# 聚合:总数量,平均年龄,最大最小
select count(*),avg(age),max(age),min(age) from user

# 分组后过滤
select user.name,count(id) todo_num
from user
left join todo on user.id = todo.user_id
group by user.id,user.name
having count(todo.id) > 2

子查询

plsql 复制代码
select * from user where id in (select user_id from todo where is_finnish=true)

CASE WHEN

plsql 复制代码
select name,age,case
when age < 25 then '青年'
when age >=25 and age <35 then '壮年'
else '中年'
end as age_type
from user

日期相关SQL

plsql 复制代码
select * from user where extract(year from create_time)=2025; # 查询2025年创建的用户
select * from user where extract(month from create_time)=3; # 某月的数据

其他

plsql 复制代码
# 字符串拼接
select concat(name,'-',age) from user

# 去重
select distinct name from user
相关推荐
倔强的石头_5 小时前
聊聊金仓KFS:一款把数据同步软件做扎实的产品
数据库
闲云野鹤在人间5 小时前
MySQL|从理论、安装、备份到主从复制、MHA高可用详解
linux·运维·数据库·mysql·云计算
禾小西5 小时前
Redis:从两大维度和三大主线建立知识体系
数据库·redis·缓存
胖头鱼的鱼缸(尹海文)6 小时前
胖头鱼的技术专栏-474 从能跑到能上线:PG SplitJSON 0.2.0 和 0.1.0 差在哪(20261008)
postgresql·json
禾小西6 小时前
Redis 数据结构:快速的 Redis 有哪些慢操作?
数据结构·数据库·redis
数据库小学妹6 小时前
数据共享交换平台选型:交换方式对比与避坑指南
数据库·信创·数据同步·数据交换平台·政务数据共享·数据共享交换平台·数据库底座
要开心吖ZSH7 小时前
MySQL 慢 SQL 排查操作手册-个人笔记版
java·笔记·sql·mysql·慢查询
loong_XL8 小时前
决策模型做内容安全检测:从踩坑到上线
数据库·安全·jev·决策模型
这个DBA有点耶8 小时前
数据库双轨并行实战:全量并行策略、增量延迟控制、双向回切与一致性校验
数据库·架构·dba
adinnet20269 小时前
保单、赔付与渠道问数:保险经营数据如何实现按需查询
大数据·数据库·人工智能