【MySQL】論理演算子の使い方:サンプル多数あり
ここではMySQLにおける論理演算子の使い方を紹介しています。
論理演算子の一覧
MySQLで利用できる論理演算子は次の通りです。
演算子 | 内容 |
AND | 論理積 |
&& | 論理積 |
OR | 論理和 |
|| | 論理和 |
NOT | 否定 |
! | 否定 |
XOR | 排他的論理和 |
論理演算子のSQLサンプル
論理積(AND・&&)
mysql> SELECT col1,col2,col3,col2 AND col3 FROM tab;
+-------+------+------+---------------+
| col1 | col2 | col3 | col2 AND col3 |
+-------+------+------+---------------+
| 00001 | 0 | 0 | 0 |
| 00002 | 1 | 0 | 0 |
| 00003 | 1 | 1 | 1 |
| 00004 | 0 | 1 | 0 |
| 00005 | 0 | NULL | 0 |
| 00006 | 1 | NULL | NULL |
| 00007 | NULL | NULL | NULL |
+-------+------+------+---------------+
7 rows in set (0.00 sec)
mysql> SELECT col1,col2,col3,col2 && col3 FROM tab;
+-------+------+------+--------------+
| col1 | col2 | col3 | col2 && col3 |
+-------+------+------+--------------+
| 00001 | 0 | 0 | 0 |
| 00002 | 1 | 0 | 0 |
| 00003 | 1 | 1 | 1 |
| 00004 | 0 | 1 | 0 |
| 00005 | 0 | NULL | 0 |
| 00006 | 1 | NULL | NULL |
| 00007 | NULL | NULL | NULL |
+-------+------+------+--------------+
7 rows in set (0.00 sec)
論理和(OR・||)
mysql> SELECT col1,col2,col3,col2 OR col3 FROM tab;
+-------+------+------+--------------+
| col1 | col2 | col3 | col2 OR col3 |
+-------+------+------+--------------+
| 00001 | 0 | 0 | 0 |
| 00002 | 1 | 0 | 1 |
| 00003 | 1 | 1 | 1 |
| 00004 | 0 | 1 | 1 |
| 00005 | 0 | NULL | NULL |
| 00006 | 1 | NULL | 1 |
| 00007 | NULL | NULL | NULL |
+-------+------+------+--------------+
7 rows in set (0.00 sec)
mysql> SELECT col1,col2,col3,col2 || col3 FROM tab;
+-------+------+------+--------------+
| col1 | col2 | col3 | col2 || col3 |
+-------+------+------+--------------+
| 00001 | 0 | 0 | 0 |
| 00002 | 1 | 0 | 1 |
| 00003 | 1 | 1 | 1 |
| 00004 | 0 | 1 | 1 |
| 00005 | 0 | NULL | NULL |
| 00006 | 1 | NULL | 1 |
| 00007 | NULL | NULL | NULL |
+-------+------+------+--------------+
7 rows in set (0.00 sec)
否定(NOT・!)
mysql> SELECT col1,col2,col3,NOT col2, NOT col3 FROM tab;
+-------+------+------+----------+----------+
| col1 | col2 | col3 | NOT col2 | NOT col3 |
+-------+------+------+----------+----------+
| 00001 | 0 | 0 | 1 | 1 |
| 00002 | 1 | 0 | 0 | 1 |
| 00003 | 1 | 1 | 0 | 0 |
| 00004 | 0 | 1 | 1 | 0 |
| 00005 | 0 | NULL | 1 | NULL |
| 00006 | 1 | NULL | 0 | NULL |
| 00007 | NULL | NULL | NULL | NULL |
+-------+------+------+----------+----------+
7 rows in set (0.00 sec)
mysql> SELECT col1,col2,col3,! col2, ! col3 FROM tab;
+-------+------+------+--------+--------+
| col1 | col2 | col3 | ! col2 | ! col3 |
+-------+------+------+--------+--------+
| 00001 | 0 | 0 | 1 | 1 |
| 00002 | 1 | 0 | 0 | 1 |
| 00003 | 1 | 1 | 0 | 0 |
| 00004 | 0 | 1 | 1 | 0 |
| 00005 | 0 | NULL | 1 | NULL |
| 00006 | 1 | NULL | 0 | NULL |
| 00007 | NULL | NULL | NULL | NULL |
+-------+------+------+--------+--------+
7 rows in set (0.00 sec)
排他的論理和(XOR)
mysql> SELECT col1,col2,col3,col2 XOR col3 FROM tab;
+-------+------+------+---------------+
| col1 | col2 | col3 | col2 XOR col3 |
+-------+------+------+---------------+
| 00001 | 0 | 0 | 0 |
| 00002 | 1 | 0 | 1 |
| 00003 | 1 | 1 | 0 |
| 00004 | 0 | 1 | 1 |
| 00005 | 0 | NULL | NULL |
| 00006 | 1 | NULL | NULL |
| 00007 | NULL | NULL | NULL |
+-------+------+------+---------------+
7 rows in set (0.00 sec)