Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2023-08-28 19:57:06
forward
955 people have browsed it

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;
Copy after login

The following is the output result:

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

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

The query is as follows:

mysql> select 1 && 1 as Result;
Copy after login

The following is the output result:

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

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;
Copy after login

The following is the output result:

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

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;
Copy after login

The following is the output result:

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

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;
Copy after login

The following is the output result:

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

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

The query is as follows:

mysql> select NULL && 1 as Result;
Copy after login

The following is the output:

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

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)
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!