MySQL where 操作符

MySql WHERE 操作符号

前言

在 WHERE 子句中,你可以使用任何条件对记录进行过滤。

准备工作

准备 users 表,并插入数据

sql 复制代码
# 创建用户表 users
create table users (
    id int AUTO_INCREMENT not null primary key ,
    name varchar(255) ,
    age int ,
    job varchar(255) ,
    address varchar(255)
);

# 插入数据
insert into users (id, name, age, job, address)
VALUES
    (null, '小赵', 18, '高中生', '广州' ),
    (null, '小钱', 19, '大学生', '广州' ),
    (null, '小孙', 20, '大学生', '广州' ),
    (null, '小李', 21, '大学生', '深圳' ),
    (null, '小张', 22, '大学生', '深圳' ),
    (null, '小吴', 23, '销售', '深圳' ),
    (null, '小赵', 24, '商务', '惠州' ),
    (null, '小王', 25, '程序员', '惠州' ),
    (null, '小冯', 26, '程序员', '惠州' );

1.逻辑运算符

符号 描述
AND 两个条件都成立
OR 两个条件中只要有一个成立
NOT 对条件进行取反操作
  • AND age > 21 并且 address = '惠州'
sql 复制代码
select * from users where age > 21 and address = '惠州'
  • OR age > 21 或者 address = '惠州'
sql 复制代码
select * from users where age > 21 or address = '惠州'
  • NOT 取反 age > 21 并且 取反 address = '深圳'
sql 复制代码
select * from users where not age > 21 and not address = '深圳'

2.比较运算符

符号 描述
= 等于
<> 不等于。注释:在 SQL 的一些版本中,该操作符可被写成 !=
> 大于
< 小于
>= 大于等于
<= 小于等于

我们以 age 举例

  • age = 21
  • age <> 21
  • age > 21
  • age < 21
  • age >= 21
  • age<= 21
sql 复制代码
select * from users where age = 21 ;
sql 复制代码
select * from users where age <> 21 ;
sql 复制代码
select * from users where age > 21 ;
sql 复制代码
select * from users where age < 21 ;
sql 复制代码
select * from users where age >= 21 ;
sql 复制代码
select * from users where age<= 21 ;

3.范围运算符

符号 描述
IN 指定针对某个列的多个可能值
NOT IN 指定针对某个列的多个不可能值
  • 我们以 address 举例 取address 列中值 为广州、 惠州的数据 |
sql 复制代码
select * from users where address in ('广州','惠州')
  • 取address 列中值 不为广州、 惠州的数据
sql 复制代码
select * from users where address not in ('广州','惠州')

结果自行练习这里就不做截图了

4.模糊查询运算符

符号 描述
LIKE 搜索某种模式
NOT LIKE 搜索某种模式,但不是该模式
REGEXP 满足匹配正则
NOT REGEXP 不满足正则条件
  • like

  • 取address 列中值 包含 广州的数据

sql 复制代码
select * from users where address like '%广州%'
  • 取address 列中值 不包含 广州的数据
sql 复制代码
select * from users where address not like '%广州%'
  • regexp 正则语法后面单独讲

  • 取address 列中值 包含 广州的数据

sql 复制代码
select * from users where address regexp '广州'

5.BETWEEN AND 运算符

符号 描述
BETWEEN AND 指定一个范围,包括两个值
NOT BETWEEN AND 指定一个范围,不包括两个值
  • between and

  • 取 age 列中值 在 20 到 25 之间的数据

sql 复制代码
select * from users where age between 20 and 25 ;
  • not between and
  • 取 age 列中值 不在 20 到 25 之间的数据
sql 复制代码
select * from users where age not between 20 and 25 ;

6.IS NULL 运算符

符号 描述
IS NULL 指定某个列的值为 NULL
IS NOT NULL 指定某个列的值不为 NULL

再次插入数据

sql 复制代码
insert into users (id, name, age, job, address)
VALUES
    (null, '小陈', 27, '程序员', null );
  • is null

  • 取address 列中值为 null 的数据

sql 复制代码
select * from users where address is null ;
  • is not null

  • 取address 列中值不为 null 的数据

sql 复制代码
select * from users where address is not null ;

|

相关推荐
tan180°5 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
DuelCode6 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
幽络源小助理6 小时前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring
爬山算法7 小时前
MySQL(116)如何监控负载均衡状态?
数据库·mysql·负载均衡
KellenKellenHao17 小时前
MySQL数据库主从复制
数据库·mysql
一只fish18 小时前
MySQL 8.0 OCP 1Z0-908 题目解析(16)
数据库·mysql
叁沐19 小时前
MySQL 07 行锁功过:怎么减少行锁对性能的影响?
mysql
Java烘焙师20 小时前
架构师必备:业务扩展模式选型
mysql·elasticsearch·架构·hbase·多维度查询
飞翔的佩奇20 小时前
Java项目:基于SSM框架实现的忘忧小区物业管理系统【ssm+B/S架构+源码+数据库+毕业论文+开题报告】
java·数据库·mysql·vue·毕业设计·ssm框架·小区物业管理系统
@Ryan Ding20 小时前
MySQL主从复制与读写分离概述
android·mysql·adb