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;

相关推荐
菜鸟plus+15 分钟前
N+1查询
java·服务器·数据库
子夜江寒17 分钟前
MySQL 表创建与数据导入导出
数据库·mysql
菜鸟小九35 分钟前
redis基础(安装配置redis)
数据库·redis·缓存
保定公民1 小时前
达梦数据库使用cp备份集恢复报错分析与解决
数据库
wd_cloud2 小时前
QT/6.7.2/Creator编译Windows64 MySQL驱动
开发语言·qt·mysql
少废话h3 小时前
Redis主从与集群搭建全指南
大数据·linux·redis·mysql
中冕—霍格沃兹软件开发测试3 小时前
测试用例库建设与管理方案
数据库·人工智能·科技·开源·测试用例·bug
The star"'3 小时前
mysql(4-7)
数据库·mysql·adb
The star"'3 小时前
mysql(1-3)
运维·mysql·云计算
jiayong233 小时前
Redis面试深度解析
数据库·redis·面试