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;

相关推荐
czhc11400756631 小时前
c# 1213
开发语言·数据库·c#
voltina1 小时前
【SQL】【事务】
数据库·sql
古渡蓝按1 小时前
PostgreSQL数据库在Windows上实现异地自动备份指南-喂饭图文教程
数据库
她说..1 小时前
MySQL数据处理(增删改)
java·开发语言·数据库·mysql·java-ee
E***U9451 小时前
从新手到入门:如何判断自己是否真的学会了 Spring Boot
数据库·spring boot·后端
Alex Gram2 小时前
MySQL实时同步到SQL Server:技术方案与实现路径
数据库·mysql
不穿格子的程序员2 小时前
Redis篇3——Redis深度剖析:内存数据的“不死之身”——RDB、AOF与混合持久化
数据库·redis·缓存·数据持久化·aof·rdb
秋深枫叶红2 小时前
嵌入式第三十四篇——linux系统编程——进程
linux·服务器·数据库·学习
贡献者手册2 小时前
SQLite 的进阶版,面向边缘计算、嵌入式场景的高性能本地数据库【Turso Database】
数据库
TH_13 小时前
6、前台界面传递老数据,导致业务数据错误
数据库