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;

相关推荐
hai74255 分钟前
在 Eclipse 的 JSP 项目中引入 MySQL 驱动
java·mysql·eclipse
Leo.yuan10 分钟前
经营分析会,该讲些什么?
大数据·数据库·数据分析
云飞扬32 分钟前
浅谈数据访问层
数据库·bpm·数据库访问层
Amarantine、沐风倩✨1 小时前
一次线上性能事故的处理复盘:从 SQL 到扩容的工程化思路
java·数据库·sql·oracle
电商API&Tina1 小时前
乐天平台 (Rakuten) 数据采集指南
大数据·开发语言·数据库·oracle·json
千寻技术帮1 小时前
10334_基于Web的文学书刊服务平台
mysql·ssm·源码·代码·文学书刊
l1t1 小时前
用SQL执行累计值汇总的几种方法
数据库·sql·postgresql·duckdb
踢足球09292 小时前
寒假打卡:2026-2-3
数据库
每次学一点2 小时前
【ZeroTier自研之路】planet的组成
服务器·网络·数据库
策知道2 小时前
2026年北京政府工作报告产业指标深度解析
大数据·数据库·人工智能·搜索引擎·政务