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;

相关推荐
hef2881 小时前
如何生成特定SQL的AWR报告_@awrsqrpt.sql深度剖析单条语句性能
jvm·数据库·python
xcjbqd02 小时前
Python API怎么加Token认证_JWT生成与验证拦截器实现
jvm·数据库·python
二月十六2 小时前
SQL Server 2022 新语法:IS [NOT] DISTINCT FROM 彻底解决 NULL 比较难题
数据库·sqlserver
~ rainbow~2 小时前
前端转型全栈(四)——常见的错误及解决方案
数据库·oracle·全栈
数厘2 小时前
2.1SQL 学习:先懂数据库概念再学 SQL
数据库·sql·学习
Cat_Rocky3 小时前
redis哨兵模式
数据库·redis
广师大-Wzx3 小时前
一篇文章看懂MySQL数据库(下)
java·开发语言·数据结构·数据库·windows·python·mysql
hef2884 小时前
golang如何使用range over func_golang range over func迭代器使用方法
jvm·数据库·python
qq_380619165 小时前
html如何查看windows
jvm·数据库·python
爱学习的小邓同学5 小时前
MySQL --- MySQL数据库基础
数据库·mysql