MySQL中比较运算符的使用
创始人
2024-01-25 11:14:49
0

MySQL中比较运算符的使用

        • 1. 加号 "+" 在MySQL中没有拼接的意思
        • 2. 只要有null值参与,运算结果都为null
        • 3. 安全等于 <=> 为null而生!!
        • 4. IS NULL 或 ISNULL() 为null的值
        • 5. IS NOT NULL 不为空的值
        • 6. least(获取最小值)
        • 7. greatest(获取最大值)
        • 8. between...and... 查询区间
        • 9. in(查询具体值的区间)
        • 10. not in(查询不在具体值的区间)
        • 11. like 模糊查询
        • 12. MySQL在Dos命令窗口清屏使用:system cls
        • 13. regexp\rlike :正则表达式

1. 加号 “+” 在MySQL中没有拼接的意思

如果是数值和字符 相+ 则字符会隐式转换为0
mysql> select 1+'hello' from dual;
+-----------+
| 1+'hello' |
+-----------+
|         1 |
+-----------+
1 row in set, 1 warning (0.04 sec)mysql>

2. 只要有null值参与,运算结果都为null

mysql> select null = null from dual;
+-----------+
| null=null |
+-----------+
|      NULL |
+-----------+
1 row in set (0.00 sec)

3. 安全等于 <=> 为null而生!!

mysql> select null <=> null from dual;
+---------------+
| null <=> null |
+---------------+
|             1 |
+---------------+
1 row in set (0.00 sec)

4. IS NULL 或 ISNULL() 为null的值

练习:查询表中commission_pct为null的数据有哪些?mysql> select last_name,salary,commission_pct from employees-> where commission_pct is null;
或
mysql> select last_name,salary,commission_pct from employees-> where isnull(commission_pct );+-------------+----------+----------------+
| last_name   | salary   | commission_pct |
+-------------+----------+----------------+
| King        | 24000.00 |           NULL |
| Hartstein   | 13000.00 |           NULL |
| Gietz       |  8300.00 |           NULL |
+-------------+----------+----------------+
......
72 rows in set (0.00 sec)

5. IS NOT NULL 不为空的值

练习:查询表中commission_pct不为null的数据有哪些?mysql> select last_name,salary,commission_pct from employees-> where commission_pct is not null;
或
mysql> select last_name,salary,commission_pct from employees-> where not commission_pct <=> null;    +------------+----------+----------------+
| last_name  | salary   | commission_pct |
+------------+----------+----------------+
| Russell    | 14000.00 |           0.40 |
| Partners   | 13500.00 |           0.30 |
| Errazuriz  | 12000.00 |           0.30 |
+------------+----------+----------------+
......
35 rows in set (0.00 sec)

6. least(获取最小值)

mysql> select least(1,2,3,4,5) from dual;+------------------+
| least(1,2,3,4,5) |
+------------------+
|                1 |
+------------------+
1 row in set (0.11 sec)

7. greatest(获取最大值)

mysql> select greatest(1,2,3,4,5) from dual;+---------------------+
| greatest(1,2,3,4,5) |
+---------------------+
|                   5 |
+---------------------+
1 row in set (0.00 sec)

8. between…and… 查询区间

(1、查询中,between 后面跟小的数,and 后面跟大的数!!!)
(2、查询结果 含头也含尾!!!)

练习:查询工资6000到8000的员工信息mysql> select employee_id,last_name,salary-> from employees-> where salary between 6000 and 8000;+-------------+-----------+---------+
| employee_id | last_name | salary  |
+-------------+-----------+---------+
|         104 | Ernst     | 6000.00 |
|         203 | Mavris    | 7500.00 |
|         179 | Johnson   | 6200.00 |
|         202 | Fay       | 8000.00 |
+-------------+-----------+---------+
......
24 rows in set (0.22 sec)
练习:查询工资不在6000到8000的员工信息mysql> select employee_id,last_name,salary-> from employees-> where salary not between 6000 and 8000;+-------------+-------------+----------+
| employee_id | last_name   | salary   |
+-------------+-------------+----------+
|         100 | King        | 24000.00 |
|         206 | Gietz       |  8300.00 |
+-------------+-------------+----------+
......
83 rows in set (0.00 sec)

9. in(查询具体值的区间)

练习:查询部门为10,20,30号部门的员工信息mysql> select last_name,salary,department_id-> from employees-> where department_id in(10,20,30);+------------+----------+---------------+
| last_name  | salary   | department_id |
+------------+----------+---------------+
| Whalen     |  4400.00 |            10 |
| Hartstein  | 13000.00 |            20 |
| Fay        |  6000.00 |            20 |
| Himuro     |  2600.00 |            30 |
| Colmenares |  2500.00 |            30 |
+------------+----------+---------------+
......
9 rows in set (0.13 sec)

10. not in(查询不在具体值的区间)

练习:查询部门工资不为6000,7000,8000的员工信息mysql> select last_name,salary,department_id-> from employees-> where salary not in(6000,7000,8000);+-------------+----------+---------------+
| last_name   | salary   | department_id |
+-------------+----------+---------------+
| King        | 24000.00 |            90 |
| Kochhar     | 17000.00 |            90 |
| De Haan     | 17000.00 |            90 |
| Hunold      |  9000.00 |            60 |
| Gietz       |  8300.00 |           110 |
+-------------+----------+---------------+
106 rows in set (0.34 sec)
......

11. like 模糊查询

(1、%代表任意字符)
(2、_代表一个字符)
(3、\ 代表转意字符)

练习1:查询last_name中包含字母 a 的员工姓名mysql>  select last_name from employees-> where last_name like '%a%';
+------------+
| last_name  |
+------------+
| Kochhar    |
| Whalen     |
| Hartstein  |
| Fay        |
| Mavris     |
| Baer       |
+------------+
.....
56 rows in set (0.00 sec)
练习2:查询第二个字符是a的员工信息mysql>  select last_name from employees-> where last_name like '_a%';
练习3:查询第二个字符是 _ 的员工信息mysql>  select last_name from employees-> where last_name like '_\_%';

12. MySQL在Dos命令窗口清屏使用:system cls

13. regexp\rlike :正则表达式

(1、^a, 匹配与a开头的字符串)

mysql> select-> 'hello_mysql' regexp '^h'-> from dual;
+---------------------------+
| 'hello_mysql' regexp '^h' |
+---------------------------+
|                         1 |
+---------------------------+
1 row in set (0.00 sec)

(2、a$, 匹配以a结尾的字符串)

mysql> select-> 'hello_mysql' rlike 'l$'-> from dual;
+--------------------------+
| 'hello_my' rlike 'y$' |
+--------------------------+
|                        1 |
+--------------------------+
1 row in set (0.00 sec)

(3、a.b 匹配以a和b之间有任意一个单字符的字符串)

mysql> select-> 'hello_java' regexp 'he..o'-> from dual;
+-----------------------------+
| 'hello_java' regexp 'he..o' |
+-----------------------------+
|                           1 |
+-----------------------------+
1 row in set (0.00 sec)

(4、[abc], 匹配包含字符 “a” 或 “b” 或 “c” 的字符串)

mysql> select-> 'huangzuang' rlike '[hz]'-> from dual;
+---------------------------+
| 'huangzuang' rlike '[hz]' |
+---------------------------+
|                         1 |
+---------------------------+
1 row in set (0.00 sec)

相关内容

热门资讯

创业板股票代码前三位(创业板股... 创业板指数今日上涨0.77%,收于2939.39点,总成交量为1453.6亿元,比上一交易日增加93...
加盟茶颜悦色奶茶店大概要多少钱... 现代中国茶叶店|店铺开业|咨询|申请|汇鑫|洛尔|备注现代中国茶叶店可以先审批年初,武汉开店计划因疫...
2020年夫妻适合的小店(20... 现在人们的经济压力越来越大,很多夫妻不愿意为别人打工,就想自己创业。毕竟,夫妻利益是一致的!但是什么...
在深圳适合一个人做的小生意,有... 现在的年轻人和中年朋友都会有创业的心,因为创业就是自由,自由工作。步入正轨后,创业的收入将超过上班的...
两元加盟店全国连锁(两元加盟店... 前阵子拼多多成功上市后,圈内有大佬分析拼多多的对手会是那些物美价廉的线下商品市场,比如地摊、跳蚤市场...
2021年一个人创业做什么项目... 2021年到底还有哪些好的创业项目呢?建议不要投资实体店。如果你现在去加盟实体店,初装费贵,人力和精...
创业板50指数成分股(创业板5... 每周市场战略观点,今晚贴一篇文章,谢谢支持!这两周外资积极流入,重点龙头股季报超预期,市场持续反弹。...
小吃加盟排行榜最火,最火的加盟... 新年伊始,就快到年底了。在这个特殊的日子里,中国加盟网结合行业动态、品牌实力、市场信誉等各个维度,编...
海景房直播暴露我军舰动态,国家...   5日,“国家安全部”微信公众号公布一则用军港当背景直播暴露我军舰动态的案例:
身无分文黑户怎样快速翻身(30... 一个人三十岁,欠了几万的债。他的人生毁了吗?有这种想法的人,你已经被废了。我以为我欠了几百万,几千万...