MySQL

WHERE 与 LIKE

MySQL 用 WHERE 筛行:比较运算、AND/OR/NOT、IN、NULL、BETWEEN,以及 LIKE 模糊匹配 % 与 _。

昨天学会了「查哪些列」,今天学会「留下哪些行」。核心就一句:

SELECT 列 FROM 表 WHERE 条件;

系列:MySQL 入门到查询 · 第 8 / 17 篇(第 8 天 · 4 月 28 日)
上一篇:SELECT 查询入门
下一篇:常用字符串函数
总目录:系列索引

环境说明

  • 库:train;表:goods(含 id、name、price)
  • 本篇专用数据(请先执行):
use train;

drop table if exists goods;
create table goods(
    id int,
    name varchar(30),
    price double
);

insert into goods(id, name, price) values
    (1, 'iPhone', 5499),
    (2, 'MacBook Pro', 8999),
    (3, 'AirPods', 899),
    (4, 'iPad', 3299),
    (5, 'Watch', 2499),
    (6, '华为手机', 4999),
    (7, '键盘', null),
    (8, 'Mac mini', 4499);

id=7 的 price 故意是 NULL,用来练空值判断。
前一日脚本可能清空过表,以本篇 seed 为准。

1. WHERE 是「选行」过滤器

select * from goods;
select * from goods where id=3;
子句作用
select 列显示哪些字段
from 表从哪张表
where 条件只保留条件为真的行

条件不成立的行被丢掉,不改表里的数据(select 只读)。

2. 比较运算符

select * from goods where id=3;
select * from goods where price>=3000;
select * from goods where price<2000;
运算符含义示例
=等于id=3
!= 或 <>不等于id!=3(入门推荐 !=)
> >=大于 / 大于等于price>=3000
< <=小于 / 小于等于price<2000

字符与日期的比较

select * from goods where name='iPhone';
  • 字符、日期比较时,值一般用引号
  • 数字直接写
-- 假设有日期列时(示意)
-- where sale_date >= '2026-01-01'

不等于

select * from goods where id!=3;
select * from goods where id<>3;

两种写法结果相同。

3. AND / OR / NOT:组合条件

AND:并且(都满足)

select * from goods where price>2000 and price<6000;
条件 A条件 BA AND B
真真真
真假假
假真假
假假假

OR:或者(满足其一)

select * from goods where id=1 or id=2;
ABA OR B
真任意真
假真真
假假假

NOT:取反(了解)

select * from goods where not price>=3000;
-- 与 price<3000 在「有数值的行」上通常一致;price 为 NULL 的行两种写法一般都不会出现

括号:先算谁

select * from goods where id=1 or id=2 and price>5000;

SQL 里 AND 优先级高于 OR(与多数编程语言类似)。拿不准就加括号:

select * from goods where (id=1 or id=2) and price>5000;
写法含义(读法)
id=1 or id=2 and price>5000id=1,或者(id=2 且价格>5000)
(id=1 or id=2) and price>5000id 是 1 或 2,并且价格>5000

4. IN:是否在「列表」里

select * from goods where id in (1,2,3);
select * from goods where id not in (1,2);
写法含义
列 in (值1, 值2, ...)列的值等于其中之一
列 not in (...)不在列表中

等价关系:

-- 下面两种结果相同
where id=1 or id=2 or id=3
where id in (1,2,3)

in 列表长的时候更干净。

5. NULL:没有值,不等于空字符串

select * from goods where price=null;        -- 通常查不到 NULL 行!
select * from goods where price is null;     -- 正确:price 是 NULL
select * from goods where price is not null; -- price 有值
写法含义
is null该列是空值
is not null该列不是空值

易错:price = null 在 SQL 里不能用来找 NULL(结果是「不确定」,行不会按你预期出现)。必须用 is null / is not null。
''(空字符串)和 NULL 不是同一个概念:'' 是有值、值为空串;NULL 是没有值。

select * from goods where price is null or price>4000;

6. BETWEEN ... AND ...:闭区间

select * from goods where price between 2000 and 5000;
写法等价
price between 2000 and 5000price>=2000 and price<=5000

注意:BETWEEN 两端都是包含的(闭区间)。

select * from goods where price not between 2000 and 5000;

NULL 不参与普通数值比较,price is null 的行一般不会被 between 选中。

7. LIKE:模糊匹配

= 是「完全一样」;LIKE 允许「像」,用通配符:

通配符含义
%任意长度任意字符(含 0 个)
_一个任意字符

包含某子串

select * from goods where name like '%Mac%';
条件含义
like 'Mac%'以 Mac 开头
like '%Pro'以 Pro 结尾
like '%Mac%'名字里含有 Mac

单个字符

-- 假设有两字商品名时,_ 表示一个字
select * from goods where name like '__';
select * from goods where name like '_ad';

中文

select * from goods where name like '%手机%';
select * from goods where name like '%键%';

LIKE 与 NOT LIKE

select * from goods where name not like '%Mac%';

等值 vs 模糊

需求用
名字就是 iPhonename='iPhone'(更直接)
名字里有 Phonename like '%Phone%'

性能了解:很大的表上,%关键词% 两边都通配时,索引往往难用上。练习无感;生产大表要谨慎,能前缀匹配就前缀匹配。入门先把结果写对。

8. 组合实战例句

-- 价格在 2000~6000,且不是 Watch
select * from goods
where price between 2000 and 6000
  and name != 'Watch';

-- id 是 1、3、5,或价格为空
select * from goods
where id in (1,3,5) or price is null;

-- 名字含 Mac,且价格有值
select * from goods
where name like '%Mac%' and price is not null;

-- 只要能显示的「便宜货」(演示:无 where 全表;有 where 只留部分)
select id, name, price from goods where price<3000;

9. 完整跟练脚本

use train;

drop table if exists goods;
create table goods(
    id int,
    name varchar(30),
    price double
);

insert into goods(id, name, price) values
    (1, 'iPhone', 5499),
    (2, 'MacBook Pro', 8999),
    (3, 'AirPods', 899),
    (4, 'iPad', 3299),
    (5, 'Watch', 2499),
    (6, '华为手机', 4999),
    (7, '键盘', null),
    (8, 'Mac mini', 4499);

select * from goods;

select * from goods where id=3;
select * from goods where price>=3000;
select * from goods where id!=3;
select * from goods where name='iPhone';

select * from goods where price>2000 and price<6000;
select * from goods where id=1 or id=2;
select * from goods where id in (1,2,3);

select * from goods where price is null;
select * from goods where price is not null;
select * from goods where price between 2000 and 5000;

select * from goods where name like '%Mac%';
select * from goods where name like '%手机%';
select * from goods where name not like '%Mac%';

10. 速查表

目标语句片段
等值where id=3
不等于where id!=3 或 <>
区间where price>=2000 and price<=5000 或 between 2000 and 5000
并且and
或者or
列表where id in (1,2,3)
空值where price is null / is not null
含有where name like '%关键字%'
前缀like 'abc%'
后缀like '%abc'
单字符like 'a_c'

11. 今天的练习清单

  • 准备本篇 goods 数据
  • where id=3、where price>=3000
  • and / or 各写一句,并加括号对比
  • in (1,2,3) 与三个 or 对比
  • price is null 找到 id=7
  • between 2000 and 5000
  • like '%Mac%'、like '%手机%'
  • 试一下 where price=null,体会「为什么找不到」

常见错误

易错:用 = null 查空值 → 应用 is null。
易错:字符/日期没加引号,或用了中文引号。
易错:and/or 优先级理解反,忘记加括号。
易错:like 写成 =,或通配符用错(* 不是 SQL 的 LIKE 通配符)。
易错:between 以为不含两端(实际含)。
易错:对 NULL 列做 >/</like 时,这些行通常不会出现,不是「丢了数据」。

小结

  1. WHERE 过滤行;SELECT 管列。
  2. 比较用 = != > <;组合用 and / or,拿不准就括号。
  3. 列表用 in;空值用 is null;区间用 between。
  4. 模糊匹配用 like + %(任意)/ _(一个字符)。
  5. 下一篇进入字符串函数:长度、截取、查找、替换。

系列导航:总目录 · 上一篇:SELECT 查询入门 · 下一篇:常用字符串函数

相关阅读

全部文章 →
← 返回列表更多「MySQL」