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;

相关推荐
夹锌饼干8 分钟前
mysql死锁排查流程--(处理mysql阻塞问题)
java·mysql
Lw老王要学习11 分钟前
CentOS 7.9达梦数据库安装全流程解析
linux·运维·数据库·centos·达梦
qq_4232339019 分钟前
Python深度学习入门:TensorFlow 2.0/Keras实战
jvm·数据库·python
无限码力36 分钟前
华为OD技术面真题 - Mysql相关 - 4
mysql·华为od·华为od技术面真题·华为od技术面八股·华为od技术面八股文·华为od技术面mysql相关
Wasim40440 分钟前
【渗透测试】SQL注入
网络·数据库·sql
laplace01231 小时前
Claude Code 逆向工程报告 笔记(学习记录)
数据库·人工智能·笔记·学习·agent·rag
2401_836563181 小时前
用Python读取和处理NASA公开API数据
jvm·数据库·python
2301_818732062 小时前
项目启动报错,错误指向xml 已解决
xml·java·数据库·后端·springboot
Austindatabases2 小时前
<span class=“js_title_inner“>DBA 未来的是末日还是希望 -- 国外专家博主分析谈发展趋势 (翻译 分析)</span>
数据库·dba
难得的我们2 小时前
超越Python:下一步该学什么编程语言?
jvm·数据库·python