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;

相关推荐
c238568 小时前
MySQL 基础用法(上):库表管理与数据增删改
c语言·数据库·c++·mysql
jnrjian9 小时前
postgres database 更换用户
数据库·postgresql
無a伟10 小时前
redis缓存完整详解
数据库·redis·缓存
G311354227311 小时前
大模型不可用时,业务还能不能继续:企业需要设计降级方案
大数据·服务器·数据库·人工智能·深度学习
VALENIAN瓦伦尼安教学设备11 小时前
激光对中仪采购要点
数据库·嵌入式硬件·算法
码农颜12 小时前
5.2.2 隔离级别
数据库·mysql
微三云 - 廖会灵 (私域系统开发)12 小时前
基于 OPC 流程控制的抖店集群 SaaS 平台设计与渠道分账系统实现
数据库
骇客野人12 小时前
MySQL 信创化迁移至 PolarDB-X 整体实施方案
数据库·mysql
计科土狗13 小时前
GESP六级专题之类与对象
java·前端·数据库
麦聪聊数据13 小时前
数据治理 ROI(下):算清投入产出比,小切口落地快速验证价值
数据库