MySQL operator!= and <> and the difference between = and <=>

Do you want to find out exactly which MySQL article you want to read? Here → MySQL Jianghu Road | Column List

1. Operator: != and <>

  • In MySQL !=and <>consistent functionality, it is recommended sql92 specification is: !=The new specification is recommended:<>

The following query for users whose username is other than "Chen Haha", the following two sentences have the same effect.

SELECT * FROM t_user WHERE username != "陈哈哈";
SELECT * FROM t_user WHERE username <> "陈哈哈";

1. Operators: = and <=>

It is worth mentioning that =, <=>as well as isthe use of the three operators

We all know that is designed to determine whether NULL, and =is used to determine all types of data other than the use of non-NULL. And <=>it is the first two together.

First provide the test table data as follows:

mysql> SELECT * from t_user;
+----+-----------+----------+
| id | username  | password |
+----+-----------+----------+
|  1 | 陈哈哈    | abcd1234 |
|  2 | 侨布斯    | 1234     |
|  3 | 提莫      | 1234abcd |
|  4 | aaa       | NULL     |
|  5 | NULL      | aaaa     |
+----+-----------+----------+
5 rows in set (0.00 sec)

The results of the following two SQL queries are the same, and data with NULL username is found

mysql> SELECT * from t_user where `username` is null;
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  5 | NULL     | aaaa     |
+----+----------+----------+
1 row in set (0.00 sec)

mysql> SELECT * from t_user where `username` <=> null;
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  5 | NULL     | aaaa     |
+----+----------+----------+
1 row in set (0.00 sec)

The results of the following two SQL queries are the same, and the data whose username is'Chen Haha' are found

mysql> SELECT * from t_user where `username` = '陈哈哈';
+----+-----------+----------+
| id | username  | password |
+----+-----------+----------+
|  1 | 陈哈哈    | abcd1234 |
+----+-----------+----------+
1 row in set (0.00 sec)

mysql> SELECT * from t_user where `username` <=> '陈哈哈';
+----+-----------+----------+
| id | username  | password |
+----+-----------+----------+
|  1 | 陈哈哈    | abcd1234 |
+----+-----------+----------+
1 row in set (0.00 sec)

It can be seen that the <=>operator is equivalent to encapsulating =and is, which can be used to judge non-NULL values ​​or NULL values.

  • <=> Only used for MySQL database, username <=> NULL0 is equivalent to username is NULL, NOT(username <=> NULL)equivalent to username is NOT NULL;
  • When col1 and col2 two columns with possible NULL values ​​need to be compared for equality, col1 <=> col2 can be used, and the null=null can also be associated.

Guess you like

Origin blog.csdn.net/qq_39390545/article/details/115331735