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;

相关推荐
谅望者几秒前
SQL子查询完全指南:从零掌握嵌套查询的三种用法与最佳实践
数据库·sql·数据库开发·子查询
阿萨德528号24 分钟前
Redis 分布式锁进阶:跨语言场景下的锁兼容性与一致性保障
数据库·redis·分布式
开开心心就好26 分钟前
电脑音质提升:杜比全景声安装详细教程
java·开发语言·前端·数据库·电脑·ruby·1024程序员节
让学习成为一种生活方式27 分钟前
调控大肠杆菌胞内ATP和NADH水平促进琥珀酸生产--文献精读172
数据库
yoi啃码磕了牙33 分钟前
Unity—Localization 多语言
java·数据库·mysql
一颗宁檬不酸1 小时前
PL/SQL 知识点总结
数据库·sql·oracle·知识点
serve the people2 小时前
Prompt Serialization in LangChain
数据库·langchain·prompt
万事大吉CC2 小时前
Win11卸载重装oracle 11g数据库
数据库
星光一影2 小时前
打车/网约车、代驾、顺风车/拼车、货运、租车等多种出行服务的一站式解决方案
mysql·微信小程序·php·uniapp·html5·web app
Jing_jing_X2 小时前
MySQL Server 启动后到底加载了什么,创建表插入数据到底怎么存的存在哪
mysql·adb