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;

相关推荐
一个处女座的程序猿O(∩_∩)O6 小时前
性能调优实战:金仓数据库连接条件下推原理与案例拆解
数据库·oracle
数据知道7 小时前
MongoDB认证机制实战:详细讲述SCRAM-SHA与X.509证书认证配置
数据库·mongodb
雷工笔记7 小时前
KingFusion 关系库查询核心:SQLQuery 与 AsynSQLQuery 函数全解析
数据库
zopple7 小时前
Knife4j文档请求异常(基于SpringBoot3,查找原因并解决)
java·服务器·数据库
执笔为剑7 小时前
docker环境升级数据库
数据库·docker·容器
sea12167 小时前
Flask配置MySQL连接信息的最佳实践
python·mysql·flask
数据知道7 小时前
MongoDB审计日志配置:详细讲述满足合规性要求的安全记录
数据库·安全·mongodb
難釋懷7 小时前
Redis搭建哨兵集群
数据库·redis·缓存
璞~7 小时前
安装达梦数据库
数据库
oioihoii8 小时前
数据库查询优化中的谓词下推策略与成本感知优化实践
服务器·数据库·oracle