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;

相关推荐
码界奇点23 分钟前
深入解析MySQL9主从复制架构详解从原理到实战优化
数据库·sql·架构·可用性测试
Miha_Singh1 小时前
查询优化综述:《A Survey of Query Optimization in Large Language Models》
数据库·人工智能·语言模型·查询优化·查询改写
打工的小王1 小时前
Redis(一)redis的下载安装与使用
数据库·redis·缓存
煎蛋学姐1 小时前
SSM医患交流m8996(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·ssm 框架·医患交流系统
麦聪聊数据2 小时前
为何“零信任”时代需要重构数据库访问层?
数据库·sql
DBA小马哥2 小时前
InfluxDB迁移?时序数据库国产替代三大难点与实践
数据库·时序数据库
LYOBOYI1232 小时前
qml程序运行逻辑
java·服务器·数据库
袁煦丞 cpolar内网穿透实验室2 小时前
mysql_exporter+cpolar远程监控 MySQL 不卡壳!cpolar 内网穿透实验室第 712 个成功挑战
服务器·数据库·mysql·远程工作·内网穿透·cpolar
Mikhail_G2 小时前
Mysql数据库操作指南(零基础篇二)
大数据·数据库·sql·mysql·数据分析
一条大祥脚2 小时前
26.1.24 分块|排序|中位数贪心+线段树二分+聚集贪心
数据库·redis·缓存