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;

相关推荐
码农阿豪1 天前
行标识符的抉择:深入理解数据库领域的OID与ROWID机制
数据库·oracle
不剪发的Tony老师1 天前
MyCLI:一个增强型MySQL命令行客户端
数据库·mysql
SHANGHAILINGEN1 天前
2400 万个未培养病毒重新定义病毒多样性
数据库·测序·组学
刘晨鑫11 天前
PostgreSQL日常维护
数据库·postgresql
xiaokangzhe1 天前
PG数据库日常应用
数据库·oracle
XDHCOM1 天前
MySQL ER_DD_VERSION_INSTALLED报错解析,数据字典版本问题,故障修复与远程处理指南
数据库·mysql
努力的小郑1 天前
Canal 不难,难的是用好:从接入到治理
后端·mysql·性能优化
yaoyouzhong1 天前
MySQL 批量插入详解:快速提升大数据导入效率的实战方法
大数据·数据库·mysql
东北甜妹1 天前
MySQL主从复制
mysql
NineData1 天前
NineData V5.0 产品发布会:让 AI 成为数据管理的驱动力,4月16日!
数据库·人工智能·ai编程