LIKE, IN and 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 によるテキストパターンの一致
= はテキストを完全に一致させてのみ判定します。パターンに一致させるには、ワイルドカード2つを用いた LIKE を使用します:
%は任意の文字列(空白を含む)を表します_はちょうど1文字を表します
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
さらに便利なテストが2つあります:
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.
Show the name of every student whose name contains the letter a. Use LIKE with %.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Show the name and score of students whose score is between 70 and 90 (inclusive). Use BETWEEN.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。