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;

相关推荐
那个失眠的夜7 小时前
AspectJ
java·开发语言·数据库·spring
亚空间仓鼠7 小时前
NoSQL数据库Redis(一):数据库基础
数据库·redis·nosql
weixin_580614007 小时前
如何防止SQL注入利用存储过程_确保存储过程不拼字符串.txt
jvm·数据库·python
james的分享7 小时前
向量数据库之Milvus
数据库·milvus
weixin_408717777 小时前
mysql权限表查询性能如何优化_MySQL系统权限缓存原理
jvm·数据库·python
吕源林7 小时前
怎么优化MongoDB的软删除设计_布尔标记与删除时间戳
jvm·数据库·python
想唱rap7 小时前
C++智能指针
linux·jvm·数据结构·c++·mysql·ubuntu·bash
吕源林7 小时前
如何解决SQL存储过程连接泄露_确保在异常后关闭连接
jvm·数据库·python
Gofarlic_OMS8 小时前
应对MathWorks合规审查的专项准备工作
大数据·服务器·网络·数据库·人工智能
七夜zippoe8 小时前
DolphinDB SQL查询:从基础到进阶
数据库·sql·进阶·聚合·dolphindb