1、运算符:!= 和 <>
下面查询username为"陈哈哈"以外的用户,以下两条语句的作用一样。
SELECT * FROM t_user WHERE username != "陈哈哈";
SELECT * FROM t_user WHERE username <> "陈哈哈";
值得一提的是 = 、 <=> 以及 is 这三个运算符的用法
大家都知道 is 专门用来判断是否为 NULL,而 = 则是用来判断非NULL以外的所有数据类型使用。而 <=> 则是前两者合起来。
先提供测试表数据如下:
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)
下面两个SQL的查询结果一致,均查出了username 为 NULL的数据
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)
下面两个SQL的查询结果一致,均查出了username 为 ‘陈哈哈’ 的数据
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)
可见,<=>运算符相当于封装了= 和 is ,既可以判断 非NULL值,也可以用来判断NULL值。
来源:blog.csdn.net/qq_39390545
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有