The IN keyword
CoddyのSQLジャーニー「基礎」セクションの一部 — レッスン 18/72。
列が複数の可能な値のいずれかと一致する行を見つける必要がある場合、複数の OR 条件を使用して記述できます。例えば、次のクエリは非常に長くなります:
SELECT *
FROM table1
WHERE col1 = 'a' OR col1 = 'b' OR col1 = 'c' OR col1 = 'd' OR ...これを IN キーワードを使って次のように簡略化できます:
SELECT *
FROM table1
WHERE col1 IN ('a', 'b', 'c', 'd', 'e', 'f')この短いバージョンは全く同じことを行います:col1 が括弧内にリストされた値のいずれかと等しい行を返します。
チャレンジ
簡単利用可能なテーブルと列:
<strong>countries</strong>:<strong>location_x</strong>,<strong>location_y</strong>,<strong>country</strong>
以下の国々のすべてのレコードを返してください:
Oman, Nicaragua, Bhutan, Senegal, Belarus
チートシート
IN を使用して、列がリスト内の任意の値と一致するかどうかをチェックします:
SELECT *
FROM table1
WHERE col1 IN ('a', 'b', 'c', 'd', 'e', 'f')これは複数の OR 条件と同等ですが、はるかに短いです。
自分で試してみよう
このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
基礎のすべてのレッスン
4More Keywords
The IN keywordThe BETWEEN keywordThe LIKE keywordThe AS keywordRecap - Cellphone Models2Conditions
Conditions BasicsThe AND keywordThe OR keywordThe NOT keywordMultiple Conditions CombinedParenthesisBooleans5Arithmetic Operations
Mathematical OperatorsMathematical ColumnsThe Modulo OperationThe ROUND() Function3Specific Return Format
Null valuesSort Results Part 1Sort Results Part 2Recap - Cyber Security FirmLimit number of recordsRecap - Vehicle Factory6Intro Challenges
Recap - Parliamentary ElectionRecap - Police Criminal ArrestRecap - Bar Beverage ContainerRecap - Engineer new columns