Home >Database >Mysql Tutorial >What is the difference between 'AND' and '&&' in MySQL?

What is the difference between 'AND' and '&&' in MySQL?

WBOY
WBOYforward
2023-08-28 19:57:06993browse

MySQL 中“AND”和“&&”的区别?

Note: There is only one difference between AND and &&, that is, AND is a standard syntax, while && is an ownership syntax.

There is no difference between AND and && except for the above statement. Let's look at all the conditions.

AND and && always result in 1 or 0. As we all know, AND and && are both logical operators. If there are multiple operands and any one of them has a value of 0, the result is 0, otherwise it is 1.

Here is a demonstration of AND and &&.

Case 1(a): If both operands are 1. Use AND.

The query is as follows:

mysql> select 1 AND 1 as Result;

The following is the output result:

+--------+
| Result |
+--------+
|      1 |
+--------+
1 row in set (0.00 sec)

Case 1(b): If both operands are 1. use&&.

The query is as follows:

mysql> select 1 && 1 as Result;

The following is the output result:

+--------+
| Result |
+--------+
|      1 |
+--------+
1 row in set (0.00 sec)

Case 2(a): If any operand is 0, then the result is 0. Use AND.

The query is as follows:

mysql> select 1 AND 0 as Result;

The following is the output result:

+--------+
| Result |
+--------+
|      0 |
+--------+
1 row in set (0.00 sec)

Case 2(b): If any operand is 0, then the result becomes 0. use&&.

The query is as follows:

mysql> select 1 && 0 as Result;

The following is the output result:

+--------+
| Result |
+--------+
|      0 |
+--------+
1 row in set (0.00 sec)

This is a null value situation.

Case 3(a): If any operand is NULL, the result becomes NULL. Use AND.

The query is as follows:

mysql> select NULL AND 1 as Result;

The following is the output result:

+--------+
| Result |
+--------+
|   NULL |
+--------+
1 row in set (0.00 sec)

Case 3(b): If any operand is NULL, the result becomes NULL. use&&.

The query is as follows:

mysql> select NULL && 1 as Result;

The following is the output:

+--------+
| Result |
+--------+
|   NULL |
+--------+
1 row in set (0.00 sec)

Note: The situation discussed above does not rely solely on 1 and 0. Any non-zero value will be true, which means if we AND or && two negative numbers, the result will become 1.

Look at the situation with negative numbers. The query is as follows:

mysql> select -10 AND -30 as Result;
+--------+
| Result |
+--------+
|      1 |
+--------+
1 row in set (0.04 sec)
mysql> select -10 && -30 as Result;
+--------+
| Result |
+--------+
|      1 |
+--------+
1 row in set (0.00 sec)

In the above case, if any value is 0, the result becomes 0 in both AND and &&. The query is as follows:

mysql> select -10 AND 0 as Result;
+--------+
| Result |
+--------+
|      0 |
+--------+
1 row in set (0.00 sec)

Look at the positive cases. The query is as follows:

mysql> select 10 AND 30 as Result;
+--------+
| Result |
+--------+
|      1 |
+--------+
1 row in set (0.00 sec)
mysql> select 10 && 30 as Result;
+--------+
| Result |
+--------+
|      1 |
+--------+
1 row in set (0.00 sec)

Among them, if any operand becomes 0, the result becomes 0. The query is as follows:

mysql> select 10 and 0 as Result;
+--------+
| Result |
+--------+
|      0 |
+--------+
1 row in set (0.00 sec)
mysql> select 10 && 0 as Result;
+--------+
| Result |
+--------+
|      0 |
+--------+
1 row in set (0.00 sec)

The above is the detailed content of What is the difference between 'AND' and '&&' in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete