SQLのorについてまとめています。
SQLのorは条件を論理和で接続する演算子

where句で複数の条件を論理和(AまたはB)で接続する場合はor演算子を使います。
以下のSQLは、employees(社員)テーブルから、姓名の名がGeorgiまたは、姓名の名がMaryのデータを20件取得するSQLです。
mysql> select emp_no, first_name, last_name from employees where first_name='Georgi' or first_name='Mary' limit 20; +--------+------------+-------------+ | emp_no | first_name | last_name | +--------+------------+-------------+ | 10001 | Georgi | Facello | | 10011 | Mary | Sluis | | 10532 | Mary | Wossner | | 10909 | Georgi | Atchley | | 11029 | Georgi | Itzfeldt | | 11430 | Georgi | Klassen | | 11821 | Mary | Piazza | | 12157 | Georgi | Barinka | | 12334 | Mary | Ertl | | 13562 | Mary | Cooley | | 13881 | Mary | Monarch | | 13924 | Mary | DasSarma | | 15220 | Georgi | Panienski | | 15660 | Georgi | Hartvigsen | | 15689 | Georgi | Capobianchi | | 15843 | Georgi | Varley | | 16021 | Mary | Ananiadou | | 16672 | Georgi | Peris | | 16939 | Georgi | Ranon | | 18453 | Georgi | Maksimenko | +--------+------------+-------------+
orを使った条件文はinを使って置き換えが可能
or条件が増えるとSQLが長くなり可読性が落ちます。inを使ってスッキリ見やすく置き換えが可能です。
【関連記事】
▶SQLでandとorを使った条件をinで置き換えるサンプルコード
mysql> select emp_no, first_name, last_name from employees
where first_name='Georgi'
or first_name='Mary'
or first_name='Kyoichi'
or first_name='Saniya' limit 20;
+--------+------------+--------------+
| emp_no | first_name | last_name |
+--------+------------+--------------+
| 10001 | Georgi | Facello |
| 10005 | Kyoichi | Maliniak |
| 10008 | Saniya | Kalloufi |
| 10011 | Mary | Sluis |
| 10532 | Mary | Wossner |
| 10909 | Georgi | Atchley |
| 10922 | Kyoichi | Wossner |
| 11029 | Georgi | Itzfeldt |
| 11208 | Saniya | Valtorta |
| 11430 | Georgi | Klassen |
| 11547 | Kyoichi | Flexer |
| 11821 | Mary | Piazza |
| 12157 | Georgi | Barinka |
| 12334 | Mary | Ertl |
| 12582 | Saniya | Herath |
| 12645 | Kyoichi | Decaestecker |
| 12796 | Saniya | Stanfel |
| 12963 | Kyoichi | Azevdeo |
| 13186 | Kyoichi | Barbanera |
| 13409 | Kyoichi | Vitiello |
+--------+------------+--------------+
20 rows in set (0.09 sec)
mysql> select emp_no, first_name, last_name from employees
where first_name in ('Georgi','Mary', 'Kyoichi','Saniya') limit 20;
また、andとorを複数使ったwhere句は、以下のように置き換えが可能です。
mysql> select * from employees
where first_name = 'Georgi' and last_name 'Peris'
or first_name = 'Mary' and last_name 'Sluis';
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date |
+--------+------------+------------+-----------+--------+------------+
| 10011 | 1953-11-07 | Mary | Sluis | F | 1990-01-22 |
| 16672 | 1955-04-25 | Georgi | Peris | M | 1986-03-13 |
+--------+------------+------------+-----------+--------+------------+
mysql> select * from employees
where (first_name, last_name) = ('Georgi','Peris')
or (first_name, last_name) = ('Mary','Sluis');
mysql> select * from employees
where (first_name, last_name) in (('Georgi','Peris'), ('Mary','Sluis'));
(first_name, last_name)のような記述方法は行コンストラクタと呼ばれています。
参考)MySQL :: MySQL 5.6 リファレンスマニュアル :: 13.2.10.5 行サブクエリー
まとめ


- or演算子は、where句の条件を論理和で接続できる
- 複数のor条件はinでまとめることができる
- orとandが混合している場合は、行コンストラクタとinを組み合わせてまとめることができる