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;

相关推荐
天竺鼠不该去劝架9 分钟前
RPA 平台选型指南(2026):金智维 vs 来也RPA vs 艺赛旗 vs 阿里云 RPA 深度对比
大数据·数据库·人工智能
tod1131 小时前
Redis 持久化机制深度解析(RDB / AOF)
数据库·redis·缓存
❀͜͡傀儡师1 小时前
一个基于PostgreSQL的轻量级消息队列(PGMQ)
数据库·postgresql·pgmq
哈库纳玛塔塔1 小时前
dbVisitor 统一数据库访问库,更新 v6.7.0,面向 AI 支持向量操作
数据库·spring boot·orm
object not found2 小时前
uniCloud 数据库:database() 和 databaseForJQL() 到底有什么区别?
数据库
zhangyueping83852 小时前
1、MYSQL-DDL
数据库·mysql
xdpcxq10292 小时前
EF Core实体追踪Entry中记录的数据
服务器·数据库·oracle
忧郁的橙子.2 小时前
02-嵌入模型和向量数据库
数据库·embedding
YIN_尹3 小时前
【MySQL】SQL里的“连连看”:从笛卡尔积到自连接
android·sql·mysql
kaoa0003 小时前
Linux入门攻坚——67、MySQL数据库-4
linux·运维·数据库·mysql