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 小时前
高频 SQL 50题(基础版)| 查询 + 连接
数据库·sql·学习·mysql
Anastasiozzzz2 小时前
阿亮随手记:动态条件生成Bean
java·前端·数据库
iameyama2 小时前
python Pandas 开发
数据库
Highcharts.js2 小时前
数据之美:用Highcharts打造专业级弧线图
javascript·数据库·highcharts·图表开发·弧线图
禹凕3 小时前
MySQL——基础知识(正则表达式)
数据库·mysql·正则表达式
SmartBrain4 小时前
FastAPI实战(第三部分):浏览历史的接口开发详解
数据库·人工智能·aigc·fastapi
山岚的运维笔记5 小时前
SQL Server笔记 -- 第77章:文件组
数据库·笔记·sql·microsoft·oracle·sqlserver
有点心急10215 小时前
Python 入门
服务器·数据库·python
独泪了无痕5 小时前
Mac Homebrew 安装 MySQL 指南
数据库·mysql·mac
l1t6 小时前
DeepSeek总结的PostgreSQL 19新功能:第二部分
数据库·postgresql