MySql where 查询条件与运算符

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: MySql where 查询条件与运算符

where 是 sql语句 中的条件关键字,类似于其他语言的额 if,只要是条件查询,就一定需要用到各种 条件运算符 来配合,我们就用下面的表来进行简单的学习测试。


mysql> select * from test; +----+------+------+ | id | name | age | +----+------+------+ | 1 | dzm | 20 | | 2 | xyq | 20 | | 3 | xyq | 20 | +----+------+------+


算数运算符:


+ - * / % 加 减 乘 除 取余


mysql> select * from test where id=1+1; +----+------+------+ | id | name | age | +----+------+------+ | 2 | xyq | 20 | +----+------+------+


比较运算符:


相等: = 不相等: <> 或 != 大于: > 大于等于: >= 小于:< 小于等于:<= ``` mysql> select * from test where id=1; +----+------+------+ | id | name | age | +----+------+------+ | 1 | dzm | 20 | +----+------+------+


mysql> select * from test where name!='dzm'; +----+------+------+ | id | name | age | +----+------+------+ | 2 | xyq | 20 | | 3 | xyq | 20 | +----+------+------+ ``` * 逻辑运算符:


逻辑与:&& 或 and 逻辑或:|| 或 or 逻辑非:! 或 not


``` mysql> select * from test where id=1 and age=20; +----+------+------+ | id | name | age | +----+------+------+ | 1 | dzm | 20 | +----+------+------+ 上面的 and 判断条件还可以写成这样 row 关键字可以省略: mysql> select * from test where (id, age) = (1, 20); mysql> select * from test where row(id, age) = (1, 20); +----+------+------+ | id | name | age | +----+------+------+ | 1 | dzm | 20 | +----+------+------+


mysql> select * from user where name='dzm' or age=20; +----+-------+------+ | id | name | age | +----+-------+------+ | 1 | dzm | 20 | +----+-------+------+


mysql> select * from test where not name='dzm'; +----+------+------+ | id | name | age | +----+------+------+ | 2 | xyq | 20 | | 3 | xyq | 20 | +----+------+------+ ```


like 模糊查询运算符:


`` like:用来判断某个字符字段的值是否包含给定的字符,如果包含则放入查询结果。 %:代表任意个数的任意字符,也可以叫通配符。 _:代表一个任意字符,也就是用一个_`下杠,就代表一个字符,多个就多个字符。


查询出 name 字段中所有完全等于 y 字符的,这么写的也就相当于 name='y',多一个少一个都不行,必须相等 select * from test where name like 'y';


查询出 name 字段中所有包含 y 字符的 select * from test where name like '%y%';


查询出 name 字段中所有以 y 字符结尾的 select * from test where name like '%y';


查询出 name 字段中所有以 y 字符开头的 select * from test where name like 'y%';


查询出 name 字段中所有刚好 3 个字符且第 2 个字符为 y 的 select * from test where name like 'y';


查询出 name 字段中所有刚好 3 个字符且第 2 个字符为 y 的 select * from test where name like '__y';


查询出 name 字段中所有刚好 2 个字符且第 1 个字符为 y 的 select * from test where name like 'y_';


查询出 name 字段中所有最少 2 个字符且第 2 个字符为 y 的,y 字符后面有N个字符都行 select * from test where name like '_y%'; ```


``` mysql> select * from test where name like '%y%'; +----+------+------+ | id | name | age | +----+------+------+ | 2 | xyq | 20 | | 3 | xyq | 20 | | 4 | djy | 5 | +----+------+------+


mysql> select * from test where name like '%m'; +----+------+------+ | id | name | age | +----+------+------+ | 1 | dzm | 20 | +----+------+------+


mysql> select * from test where name like '__m'; +----+------+------+ | id | name | age | +----+------+------+ | 1 | dzm | 20 | +----+------+------+


mysql> select * from test where name like '_z%'; +----+------+------+ | id | name | age | +----+------+------+ | 1 | dzm | 20 | +----+------+------+ ``` between 范围限定运算符:


``` between:用于判断某个字段的值是否在给定的两个数据范围之间。


select * from test where id between 2 and 3; 其实就相当于: select * from test where id>=2 and id<=3; ```


``` mysql> select * from test where id between 2 and 3; +----+------+------+ | id | name | age | +----+------+------+ | 2 | xyq | 20 | | 3 | xyq | 20 | +----+------+------+ mysql> select * from test where id between 2 and 1; Empty set (0.00 sec)


也相当于: mysql> select * from test where id>=2 and id<=3; +----+------+------+ | id | name | age | +----+------+------+ | 2 | xyq | 20 | | 3 | xyq | 20 | +----+------+------+ ```


in 运算符:


``` in:用于判断某个字段的值是否存在指定的值列表中,存在就算成立。


相当于 name 字段只要存在 in 后面的列表中即可,也可以说 in 后面的数组包含 name 字段值即可。 select * from test where name in ('d', 'z', 'm', 'dzm'); ```


``` mysql> select * from test where name in ('d', 'z', 'm'); Empty set (0.00 sec) mysql> select * from test where name in ('d', 'z', 'm', 'dzm'); +----+------+------+ | id | name | age | +----+------+------+ | 1 | dzm | 20 | +----+------+------+


也相当于: mysql> select * from test where name='d' or name='z' or name='m' or name='dzm'; +----+------+------+ | id | name | age | +----+------+------+ | 1 | dzm | 20 | +----+------+------+ ```


is 运算符:


``` is: 用于判断一个字段值是否存在或不存在,它只有两种写法,要么 is null 要么 is not null: select * from test where name is null; select * from test where name is not null;


null 与 not null 必须用 is 来判断,is 也是专用于这两个值使用,不能写成: select * from test where name=null; select * from test where name=not null;


测试数据: mysql> select * from test; +----+------+------+ | id | name | age | +----+------+------+ | 1 | dzm | 20 | | 2 | xyq | 20 | | 3 | xyq | 20 | | 4 | djy | 5 | | 5 | NULL | NULL | +----+------+------+ ```


``` mysql> select * from test where name is null; +----+------+------+ | id | name | age | +----+------+------+ | 5 | NULL | NULL | +----+------+------+


mysql> select * from test where name is not null; +----+------+------+ | id | name | age | +----+------+------+ | 1 | dzm | 20 | | 2 | xyq | 20 | | 3 | xyq | 20 | | 4 | djy | 5 | +----+------+------+ ```


相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
4月前
|
缓存 关系型数据库 MySQL
MySQL索引策略与查询性能调优实战
在实际应用中,需要根据具体的业务需求和查询模式,综合运用索引策略和查询性能调优方法,不断地测试和优化,以提高MySQL数据库的查询性能。
386 66
|
15天前
|
算法 关系型数据库 MySQL
join查询可以⽆限叠加吗?MySQL对join查询有什么限制吗?
大家好,我是 V 哥。本文详细探讨了 MySQL 中 `JOIN` 查询的限制及其优化方法。首先,`JOIN` 查询不能无限叠加,存在资源(CPU、内存、磁盘 I/O)、性能和语法等方面的限制。过多的 `JOIN` 操作会导致数据库性能急剧下降。其次,介绍了三种常见的 `JOIN` 查询算法:嵌套循环连接(NLJ)、索引嵌套连接(INL)和基于块的嵌套循环连接(BNL),并分析了它们的触发条件和性能特点。最后,分享了优化 `JOIN` 查询的方法,包括 SQL 语句优化、索引优化、数据库配置调整等。关注 V 哥,了解更多技术干货,点赞👍支持,一起进步!
|
3月前
|
SQL 关系型数据库 MySQL
【MySQL基础篇】多表查询(隐式/显式内连接、左/右外连接、自连接查询、联合查询、标量/列/行/表子查询)
本文详细介绍了MySQL中的多表查询,包括多表关系、隐式/显式内连接、左/右外连接、自连接查询、联合查询、标量/列/行/表子查询及其实现方式,一文全面读懂多表联查!
【MySQL基础篇】多表查询(隐式/显式内连接、左/右外连接、自连接查询、联合查询、标量/列/行/表子查询)
|
3月前
|
存储 Oracle 关系型数据库
索引在手,查询无忧:MySQL索引简介
MySQL 是一款广泛使用的关系型数据库管理系统,在2024年5月的DB-Engines排名中得分1084,仅次于Oracle。本文介绍MySQL索引的工作原理和类型,包括B+Tree、Hash、Full-text索引,以及主键、唯一、普通索引等,帮助开发者优化查询性能。索引类似于图书馆的分类系统,能快速定位数据行,极大提高检索效率。
90 8
|
2月前
|
缓存 关系型数据库 MySQL
【深入了解MySQL】优化查询性能与数据库设计的深度总结
本文详细介绍了MySQL查询优化和数据库设计技巧,涵盖基础优化、高级技巧及性能监控。
493 0
|
3月前
|
SQL 关系型数据库 MySQL
MySQL 窗口函数详解:分析性查询的强大工具
MySQL 窗口函数从 8.0 版本开始支持,提供了一种灵活的方式处理 SQL 查询中的数据。无需分组即可对行集进行分析,常用于计算排名、累计和、移动平均值等。基本语法包括 `function_name([arguments]) OVER ([PARTITION BY columns] [ORDER BY columns] [frame_clause])`,常见函数有 `ROW_NUMBER()`, `RANK()`, `DENSE_RANK()`, `SUM()`, `AVG()` 等。窗口框架定义了计算聚合值时应包含的行。适用于复杂数据操作和分析报告。
190 11
|
3月前
|
存储 关系型数据库 MySQL
mysql怎么查询longblob类型数据的大小
通过本文的介绍,希望您能深入理解如何查询MySQL中 `LONG BLOB`类型数据的大小,并结合优化技术提升查询性能,以满足实际业务需求。
214 6
|
2月前
|
关系型数据库 MySQL 数据库连接
数据库连接工具连接mysql提示:“Host ‘172.23.0.1‘ is not allowed to connect to this MySQL server“
docker-compose部署mysql8服务后,连接时提示不允许连接问题解决
|
1月前
|
关系型数据库 MySQL 数据库
Docker Compose V2 安装常用数据库MySQL+Mongo
以上内容涵盖了使用 Docker Compose 安装和管理 MySQL 和 MongoDB 的详细步骤,希望对您有所帮助。
167 42
|
24天前
|
关系型数据库 MySQL 网络安全
如何排查和解决PHP连接数据库MYSQL失败写锁的问题
通过本文的介绍,您可以系统地了解如何排查和解决PHP连接MySQL数据库失败及写锁问题。通过检查配置、确保服务启动、调整防火墙设置和用户权限,以及识别和解决长时间运行的事务和死锁问题,可以有效地保障应用的稳定运行。
124 25