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;

相关推荐
M-bao2 分钟前
缓存与数据库一致性方案
数据库·缓存
一念春风17 分钟前
C# 音频分离(MP3伴奏)
数据库·c#·音视频
uncleqiao42 分钟前
11、认识redis的sentinel
数据库·redis
猫头虎1 小时前
浅谈国产数据库多租户方案:提升云计算与SaaS的资源管理效率
大数据·数据库·数据仓库·sql·云计算·时序数据库·kingbasees
我在北国不背锅1 小时前
JDBC插件式数据库连接器
java·数据库·jdbc
yinzhiqing1 小时前
ubuntu24设置拼音输入法,解决chrome不能输入中文
前端·数据库·chrome
annus mirabilis2 小时前
使用n8n构建自动化工作流:从数据库查询到邮件通知的使用指南
运维·数据库·自动化·n8n
思逻辑维2 小时前
数据库+Docker+SSH三合一!深度评测HexHub的全栈开发体验
数据库·docker·ssh·软件工程·开源软件·软件需求
微辣已是极限2 小时前
mysql日常巡检
数据库·mysql·dba
老苏畅谈运维3 小时前
PostgreSQL的dblink扩展模块使用方法
数据库·postgresql