MySQL logical operator

Source: Internet
Author: User

Operator Role
Not OR! Logical Non-
And OR && Logic and
OR OR | | Logical OR
Xor Logical XOR or

1, logical non (not OR!) )

(1) When the operand is 0 o'clock, the resulting value is 1
(2) When the operand is not 0 o'clock, the resulting value is 0
(3) When the operand is null, the resulting value is null

Mysql> SELECT not ten,!10, not (1-1),! (1-1), not +, not NULL;
+--------+-----+-----------+--------+---------+----------+
| Not 10 |!10 | Not (1-1) |! (1-1) | Not | Not NULL |
+--------+-----+-----------+--------+---------+----------+
| 0 | 0 | 1 | 1 | 0 | NULL |
+--------+-----+-----------+--------+---------+----------+

2. Logic and (and or &&)

(1) When all operands are non-0 values and are not NULL, the resulting value is 1
(2) when one or more operands is 0 o'clock, the resulting value is 0
(3) The remainder of the value is NULL

Mysql> SELECT 1 AND-1, 1 && 0, 0 and NULL, 1 && null;
+----------+--------+------------+-----------+
| 1 AND-1 | 1 && 0 | 0 and NULL | 1 && NULL |
+----------+--------+------------+-----------+
| 1 | 0 | 0 | NULL |
+----------+--------+------------+-----------+

3. Logic or (or | |)

(1) When two operands are non-NULL values, and any one operand is not a 0 value, the result is 1, otherwise 0
(2) When one operand is null and the other operand is a non-0 value, the result is 1, otherwise the result is null
(3) When two operands are null, the resulting result is null

mysql> SELECT 1 OR-1 OR 0, 1 | | 2, 0 OR null, NULL | | NULL;
+--------------+--------+-----------+--------------+
| 1 OR-1 OR 0 | 1 | | 2 | 0 OR NULL | NULL | | NULL |
+--------------+--------+-----------+--------------+
| 1 | 1 | NULL | NULL |
+--------------+--------+-----------+--------------+

4. Logical XOR (XOR)

(1) The calculation of a XOR B is equivalent to (A and (not B)) or ((not a) and b)
(2) When either operand is null, the return value is null
(3) for non-NULL operands, if two operands are non-0 values or 0 values, the result is 0
(4) If one is a value of 0 and the other is a non-0 value, the return result is 1

Mysql> SELECT 1 xor 1, 0 xor 0, 1 XOR 0, 1 xor NULL, 1 xor 1 XOR 1;
+---------+---------+---------+------------+---------------+
| 1 XOR 1 | 0 XOR 0 | 1 XOR 0 | 1 XOR NULL | 1 XOR 1 XOR 1 |
+---------+---------+---------+------------+---------------+
| 0 | 0 | 1 | NULL | 1 |
+---------+---------+---------+------------+---------------+

MySQL logical operator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.