MySQL操作 UPDATE、SELECT、UNION、IN、DISTINCT

update 更新

所有人的年龄加一:update user set age=age+1;

只更新某个: update user set age=age+1 where name='zhang san';

select查询

powershell 复制代码
select * from user;//一般不建议使用通配符
select name,age,sex from user;//根据键查找
select name,age,sex from user where sex='M' and age>=20 and age<=25;
select name,age,sex from user where sex='M' and age between 20 and 25;
select name,age,sex from user where sex='W' or age>=22;
select name,age,sex from user where name like "zhang%";
select name,age,sex from user where name is not null;

联合查询 UNION

powershell 复制代码
SELECT expression1, expression2, ... expression_n
FROM tables[WHERE conditions]
UNION [ALL | DISTINCT]
SELECT expression1, expression2, ... expression_n
FROM tables[WHERE conditions]

union默认去重,不用修饰distinct,all表示显示所有重复值

例如:

powershell 复制代码
select name,age,sex from user where age >=21 union all select name,age,sex from user where sex='M';

注意:between是 。like 部分匹配不加=,null not null使用is

带in子查询

powershell 复制代码
select name,age,sex from user where age in (20,21);
select name,age,sex from user where age not in (20,21);

在一个区间内查找IN,不在这个区间内查找NOT IN.

去重

将年龄拿出来去重显示: select distinct age from user;

相关推荐
昌原的儿子LEO15 小时前
Linux IO多路复用与SQLite数据库核心知识点总结
linux·数据库·oracle
山岚的运维笔记15 小时前
mysql 专业笔记 -- 第 8 章:使用变量
运维·数据库·笔记·后端·学习·mysql·dba
聚美智数15 小时前
网安查询-备案查询-网络安全查询API接口介绍
网络·数据库·web安全
坐吃山猪16 小时前
【多线程】Semaphore使用
java·数据库·oracle·多线程
数智启示录16 小时前
PostgreSQL 内存调优实战(第 12 篇):work_mem 只调大 64 倍,峰值为什么远不止 64 倍
运维·数据库·经验分享·postgresql·面试
姜太小白17 小时前
【Oracle】记排查sh脚本调用SQL执行卡顿的排查总结
数据库·sql·oracle
bgy666617 小时前
MariaDB 数据库 基础详解
数据库·mariadb
自动化监测Learner17 小时前
QGIS 把geojson数据导入 PostgreSQL/PostGIS 的完整流程
数据库·postgresql
虎虎(_ _)。゜zzZ17 小时前
Qdrant向量数据库工程实战
数据库·人工智能·大模型·向量数据库·rag·qdrant
ZGG00318 小时前
线程池详解:从 7 大参数到生产避坑
java·数据库·后端