Mysql可以通过运算符来对表中数据进行运算,比如通过出生日期求年龄等
运算符包括四类,分别是:算数运算符、比较运算符、逻辑运算符和位运算符
算数运算符
加、减、乘运算
复制代码 代码如下:
mysql> select a,a+5,a*2 from t1;
+------+------+------+
| a | a+5 | a*2 |
+------+------+------+
| 24 | 29 | 48 |
+------+------+------+
row in set (0.00 sec)
这里的原值为24,后面也可以使用混合运算,只需要注意优先级即可
除法和取模运算
复制代码 代码如下:
mysql> select a,a/3,a div 3,a%5,mod(a,5) from t1;
+------+--------+---------+------+----------+
| a | a/3 | a div 3 | a%5 | mod(a,5) |
+------+--------+---------+------+----------+
| 24 | 8.0000 | 8 | 4 | 4 |
+------+--------+---------+------+----------+
row in set (0.00 sec)
此处 / 和 div 代表整除,% 和 mod 代表取模
要注意的是,如果被除数为0,那么计算结果是NULL
比较运算符
数值比较
复制代码 代码如下:
mysql> select a,a=24,a<12,a>40,a>=24,a<=24,a!=24,a<>24,a<=>24 from t1;
+------+------+------+------+-------+-------+-------+-------+--------+
| a | a=24 | a<12 | a>40 | a>=24 | a<=24 | a!=24 | a<>24 | a<=>24 |
+------+------+------+------+-------+-------+-------+-------+--------+
| 24 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 |
+------+------+------+------+-------+-------+-------+-------+--------+
row in set (0.00 sec)
这里的1代表真,0代表假,需要说明的是<>和<=>
<>代表不等于,等同于!=
<=>代表等于,等同于=
此外,等于和不等于不仅可以比较数值,还能比较字符串
字符串比较
复制代码 代码如下:
mysql> select a,a='24','ha'<>'ha','xa'='xa','b'!='b' from t1;
+------+--------+------------+-----------+----------+
| a | a='24' | 'ha'<>'ha' | 'xa'='xa' | 'b'!='b' |
+------+--------+------------+-----------+----------+
| 24 | 1 | 0 | 1 | 0 |
+------+--------+------------+-----------+----------+
row in set (0.00 sec)
is null 和is not null
复制代码 代码如下:
mysql> select a,a is null, a is not null from t1;
+------+-----------+---------------+
| a | a is null | a is not null |
+------+-----------+---------------+
| 24 | 0 | 1 |
+------+-----------+---------------+
row in set (0.00 sec)
这里可以判断是否为空,NULL也可以跟NULL比较
between and和not between and
复制代码 代码如下:
mysql> select a,a between 15 and 30,a not between 15 and 30 from t1;
+------+---------------------+-------------------------+
| a | a between 15 and 30 | a not between 15 and 30 |
+------+---------------------+-------------------------+
| 24 | 1 | 0 |
+------+---------------------+-------------------------+
row in set (0.00 sec)
between and 和not between and可以判断数值是否在某一区间内
in
mysql> select a,a in(1,2,23),a in(24,12,22) from t1;
+------+--------------+----------------+
| a | a in(1,2,23) | a in(24,12,22) |
+------+--------------+----------------+
| 24 | 0 | 1 |
+------+--------------+----------------+
row in set (0.00 sec)
判断操作数是否在某一集合内
like
复制代码 代码如下:
mysql> select s,s like 'beijing',s like 'b%g',s like 'bei____',s like '%jing' from t2;
+---------+------------------+--------------+------------------+----------------+
| s | s like 'beijing' | s like 'b%g' | s like 'bei____' | s like '%jing' |
+---------+------------------+--------------+------------------+----------------+
| beijing | 1 | 1 | 1 | 1 |
+---------+------------------+--------------+------------------+----------------+
row in set (0.00 sec)
ike可以用来匹配字符串,_代表单个字符,%代表多个字符
逻辑运算符
与运算
复制代码 代码如下:
mysql> select 2&&2,2&&null,2 and 3,2 and 2;
+------+---------+---------+---------+
| 2&&2 | 2&&null | 2 and 3 | 2 and 2 |
+------+---------+---------+---------+
| 1 | NULL | 1 | 1 |
+------+---------+---------+---------+
row in set (0.00 sec)
这里&&和and意思一样
或运算
复制代码 代码如下:
mysql> select 2||2,2||null,2 or 3,2 or 0;
+------+---------+--------+--------+
| 2||2 | 2||null | 2 or 3 | 2 or 0 |
+------+---------+--------+--------+
| 1 | 1 | 1 | 1 |
+------+---------+--------+--------+
row in set (0.00 sec)
这里||和or的意思一样
非运算
复制代码 代码如下:
mysql> select !1,!2,!null;
+----+----+-------+
| !1 | !2 | !null |
+----+----+-------+
| 0 | 0 | NULL |
+----+----+-------+
row in set (0.00 sec)
此外还有位运算,目前还没用到,等用到的时候再补上
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]