MySQL Tutorial/Logic Operator/OR

Материал из SQL эксперт
Версия от 09:50, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Logical OR (OR( || ))

When both operands are non-NULL, the result is 1 if any operand is non-zero, and 0 otherwise.

With a NULL operand, the result is 1 if the other operand is non-zero, and NULL otherwise.

If both operands are NULL, the result is NULL.



mysql>
mysql> SELECT 1 || 1;
+--------+
| 1 || 1 |
+--------+
|      1 |
+--------+
1 row in set (0.00 sec)
mysql> SELECT 1 || 0;
+--------+
| 1 || 0 |
+--------+
|      1 |
+--------+
1 row in set (0.00 sec)
mysql> SELECT 0 || 0;
+--------+
| 0 || 0 |
+--------+
|      0 |
+--------+
1 row in set (0.00 sec)


Using Logical OR operator with NULL value

mysql>
mysql> SELECT 0 || NULL;
+-----------+
| 0 || NULL |
+-----------+
|      NULL |
+-----------+
1 row in set (0.00 sec)
mysql> SELECT 1 || NULL;
+-----------+
| 1 || NULL |
+-----------+
|         1 |
+-----------+
1 row in set (0.02 sec)
mysql> SELECT NULL || NULL;
+--------------+
| NULL || NULL |
+--------------+
|         NULL |
+--------------+
1 row in set (0.00 sec)
mysql>
mysql>


Using OR to link two conditions

mysql>
mysql>
mysql> CREATE TABLE Books(
    ->    BookID SMALLINT NOT NULL PRIMARY KEY,
    ->    BookName VARCHAR(40) NOT NULL,
    ->    Category VARCHAR(15),
    ->    InStock SMALLINT NOT NULL,
    ->    OnOrder SMALLINT NOT NULL
    -> );
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql>
mysql> INSERT INTO Books VALUES (101, "Java",           "Nonfiction", 12, 13),
    ->                          (102, "MySQL",          "Fiction",    17, 20),
    ->                          (103, "Oracle",         "Nonfiction", 23, 33),
    ->                          (104, "VB.net",         "Nonfiction", 32, 12),
    ->                          (105, "www.sqle.ru", "Fiction",    6,  35),
    ->                          (106, "Perl",           "Fiction",    28, 14),
    ->                          (107, "Php",             NULL,        46, 3);
Query OK, 7 rows affected (0.00 sec)
Records: 7  Duplicates: 0  Warnings: 0
mysql>
mysql>
mysql> select * from Books;
+--------+----------------+------------+---------+---------+
| BookID | BookName       | Category   | InStock | OnOrder |
+--------+----------------+------------+---------+---------+
|    101 | Java           | Nonfiction |      12 |      13 |
|    102 | MySQL          | Fiction    |      17 |      20 |
|    103 | Oracle         | Nonfiction |      23 |      33 |
|    104 | VB.net         | Nonfiction |      32 |      12 |
|    105 | www.sqle.ru | Fiction    |       6 |      35 |
|    106 | Perl           | Fiction    |      28 |      14 |
|    107 | Php            | NULL       |      46 |       3 |
+--------+----------------+------------+---------+---------+
7 rows in set (0.00 sec)
mysql>
mysql> SELECT BookName, Category, InStock, OnOrder
    -> FROM Books
    -> WHERE InStock>30 OR OnOrder>30
    -> ORDER BY BookName;
+----------------+------------+---------+---------+
| BookName       | Category   | InStock | OnOrder |
+----------------+------------+---------+---------+
| Oracle         | Nonfiction |      23 |      33 |
| Php            | NULL       |      46 |       3 |
| VB.net         | Nonfiction |      32 |      12 |
| www.sqle.ru | Fiction    |       6 |      35 |
+----------------+------------+---------+---------+
4 rows in set (0.00 sec)
mysql>
mysql> drop table Books;
Query OK, 0 rows affected (0.03 sec)
mysql>