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;

相关推荐
W***83205 小时前
如何在 Ubuntu 22.04 上安装 MySQL
linux·mysql·ubuntu
老华带你飞5 小时前
海产品销售系统|海鲜商城购物|基于SprinBoot+vue的海鲜商城系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·海鲜商城购物系统
合作小小程序员小小店5 小时前
桌面开发,在线%超市销售管理%系统,基于vs2022,c#,winform,sql server数据
开发语言·数据库·microsoft·c#
SelectDB6 小时前
字节跳动:Apache Doris + AI 一站式融合数据引擎的探索与实践
数据库·apache
7***q6086 小时前
在linux(Centos)中Mysql的端口修改保姆级教程
linux·mysql·centos
IvorySQL6 小时前
PostgreSQL 18 - 时间约束 (Temporal Constraints)
数据库·postgresql·开源
q***61416 小时前
从MySQL迁移到PostgreSQL的完整指南
数据库·mysql·postgresql
q***82916 小时前
windows同时安装两个不同版本的Mysql
windows·mysql·adb
N***73857 小时前
SQL锁机制
java·数据库·sql
小羊在奋斗7 小时前
MySQL表的约束:从基础到核心(附场景+案例)
android·数据库·mysql