MySql where 查询条件与运算符

本文涉及的产品
RDS AI 助手,专业版
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS DuckDB + QuickBI 企业套餐,8核32GB + QuickBI 专业版
简介: 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 | +----+------+------+ ```


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
7月前
|
SQL 缓存 监控
MySQL缓存机制:查询缓存与缓冲池优化
MySQL缓存机制是提升数据库性能的关键。本文深入解析了MySQL的缓存体系,包括已弃用的查询缓存和核心的InnoDB缓冲池,帮助理解缓存优化原理。通过合理配置,可显著提升数据库性能,甚至达到10倍以上的效果。
|
7月前
|
SQL 存储 关系型数据库
MySQL体系结构详解:一条SQL查询的旅程
本文深入解析MySQL内部架构,从SQL查询的执行流程到性能优化技巧,涵盖连接建立、查询处理、执行阶段及存储引擎工作机制,帮助开发者理解MySQL运行原理并提升数据库性能。
|
7月前
|
SQL 关系型数据库 MySQL
MySQL的查询操作语法要点
储存过程(Stored Procedures) 和 函数(Functions) : 储存过程和函数允许用户编写 SQL 脚本执行复杂任务.
294 14
|
7月前
|
SQL 关系型数据库 MySQL
MySQL的查询操作语法要点
以上概述了MySQL 中常见且重要 的几种 SQL 查询及其相关概念 这些知识点对任何希望有效利用 MySQL 进行数据库管理工作者都至关重要
192 15
|
7月前
|
SQL 监控 关系型数据库
SQL优化技巧:让MySQL查询快人一步
本文深入解析了MySQL查询优化的核心技巧,涵盖索引设计、查询重写、分页优化、批量操作、数据类型优化及性能监控等方面,帮助开发者显著提升数据库性能,解决慢查询问题,适用于高并发与大数据场景。
|
7月前
|
SQL 关系型数据库 MySQL
MySQL入门指南:从安装到第一个查询
本文为MySQL数据库入门指南,内容涵盖从安装配置到基础操作与SQL语法的详细教程。文章首先介绍在Windows、macOS和Linux系统中安装MySQL的步骤,并指导进行初始配置和安全设置。随后讲解数据库和表的创建与管理,包括表结构设计、字段定义和约束设置。接着系统介绍SQL语句的基本操作,如插入、查询、更新和删除数据。此外,文章还涉及高级查询技巧,包括多表连接、聚合函数和子查询的应用。通过实战案例,帮助读者掌握复杂查询与数据修改。最后附有常见问题解答和实用技巧,如数据导入导出和常用函数使用。适合初学者快速入门MySQL数据库,助力数据库技能提升。
|
7月前
|
SQL 监控 关系型数据库
MySQL高级查询技巧:子查询、联接与集合操作
本文深入解析了MySQL高级查询的核心技术,包括子查询、联接和集合操作,通过实际业务场景展示了其语法、性能差异和适用场景,并提供大量可复用的代码示例,助你从SQL新手进阶为数据操作高手。
|
7月前
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
493 158
|
7月前
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
7月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1242 152

推荐镜像

更多
下一篇
开通oss服务