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 分钟前
SQLite数据库的介绍与使用
数据库·python
2401_8384725124 分钟前
用Python和Twilio构建短信通知系统
jvm·数据库·python
石头wang30 分钟前
oracle jdbc 依赖以及对dbeaver的性能影响,如何选择oracle驱动, oracle jdbc 驱动
数据库·oracle
weixin_4521595532 分钟前
如何从Python初学者进阶为专家?
jvm·数据库·python
2301_790300961 小时前
用Python读取和处理NASA公开API数据
jvm·数据库·python
万象.1 小时前
redis持久化:AOF和RDB
数据库·redis·缓存
tod1132 小时前
力扣高频 SQL 50 题阶段总结(四)
开发语言·数据库·sql·算法·leetcode
!chen2 小时前
Redis快速实现布隆过滤器
数据库·redis·缓存
2301_790300962 小时前
数据分析与科学计算
jvm·数据库·python
-XWB-2 小时前
【Oracle】Oracle诊断系列(2/6):锁问题与阻塞分析——解决“卡住”的会话
数据库·oracle