LIKE, IN and BETWEEN · LIKE、IN 与 BETWEEN
Matching text patterns with LIKE
= only matches text exactly. To match a pattern, use LIKE with two wildcards:
%stands for any run of characters (including none)_stands for exactly one character
'S%' means "starts with S". '%a%' means "contains an a". '_o%' means "an o as the second letter".
用 LIKE 匹配文本模式
= 只能精确匹配文本。要匹配一个模式,用 LIKE 配合两个通配符:
%代表任意一串字符(也可以是零个)_代表恰好一个字符
SELECT name FROM student WHERE name LIKE 'S%';
'S%' 表示“以 S 开头”。'%a%' 表示“包含一个 a”。'_o%' 表示“第二个字母是 o”。
Lists and ranges: IN and BETWEEN
Two more handy tests:
IN (…)matches any value in a list:WHERE form IN ('11A', '11C')BETWEEN low AND highmatches a range, including both ends:
Both are shorter than writing several ORs.
列表与区间:IN 和 BETWEEN
还有两个好用的测试:
IN (…)匹配列表中的任意一个值:WHERE form IN ('11A', '11C')BETWEEN low AND high匹配一个区间,含两端:
SELECT name, score FROM student WHERE score BETWEEN 70 AND 90;
两者都比写好几个 OR 更简短。
Common mistakes
- In
LIKE,%matches any text:'A%'means "starts with A". BETWEEN a AND bincludes both ends.
常见错误
- 在
LIKE中,%匹配任意文本:'A%'表示“以 A 开头”。 BETWEEN a AND b两端都包含。
Pattern & range filters · 模式与范围过滤
LIKE / IN / BETWEEN are all WHERE — they keep matching rows. · LIKE / IN / BETWEEN 都是 WHERE——保留匹配的行。
Show the name of every student whose name contains the letter a. Use LIKE with %. · 显示名字中包含字母 a 的每名学生的 name。用 LIKE 配合 %。
Click Run to see the output here. · 点击“运行”查看此处输出。
Show the name and score of students whose score is between 70 and 90 (inclusive). Use BETWEEN. · 显示分数在 70 到 90 之间(含两端)的学生的 name 和 score。用 BETWEEN。
Click Run to see the output here. · 点击“运行”查看此处输出。