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;

相关推荐
stark张宇16 分钟前
MySQL 核心内幕:从索引原理、字段选型到日志机制与外键约束,一篇打通数据库任督二脉
数据库·mysql·架构
倔强的石头_23 分钟前
融合数据库架构实践:关系型、JSON与全文检索的“一库多能”深度解析
数据库
星辰员2 小时前
KingbaseES数据库:ksql 命令行用户与权限全攻略,从创建到删除
数据库
华仔啊16 小时前
千万别给数据库字段加默认值 null!真的会出问题
java·数据库·后端
Hoffer_1 天前
MySQL 强制索引:USE/FORCE INDEX 用法与避坑
后端·mysql
Hoffer_1 天前
MySQL 索引核心操作:CREATE/DROP/SHOW
后端·mysql
随风飘的云2 天前
MySQL的慢查询优化解决思路
数据库
IvorySQL2 天前
PostgreSQL 技术日报 (3月7日)|生态更新与内核性能讨论
数据库·postgresql·开源
赵渝强老师2 天前
【赵渝强老师】金仓数据库的数据文件
数据库·国产数据库·kingbase·金仓数据库
随逸1772 天前
《Milvus向量数据库从入门到实战,手把手搭建语义检索系统》
数据库