云计算百科
云计算领域专业知识百科平台

MySQL的运算符与表达式(基础版,新手教学)

1.算术运算符

(1).+加法运算

select 5+3;

运算结果

+—–+
| 3+5 |
+—–+
| 8 |
+—–+
1 row in set (0.01 sec)

(2).- 减法运算

select 5-3-2;
运算结果
+——-+
| 5-3-2 |
+——-+
| 0 |
+——-+
1 row in set (0.00 sec)

(3).*乘法运算

select 3*5;
运算结果
+—–+
| 3*5 |
+—–+
| 15 |
+—–+
1 row in set (0.00 sec)

(4).除法运算

select 3/5;
运算结果
+——–+
| 3/5 |
+——–+
| 0.6000 |
+——–+
1 row in set (0.01 sec)

(5)div运算也是除法运算,但是只取整数

select 7 div 5;
运算结果
+———+
| 7 div 5 |
+———+
| 1 |
+———+
1 row in set (0.00 sec)

(6).取余运算%

select 7%5;
运算结果
+——+
| 7%5 |
+——+
| 2 |
+——+
1 row in set (0.00 sec)

(7).mod运算(同作用%一样)

select 12 mod 5;
运算结果
+———-+
| 12 mod 5 |
+———-+
| 2 |
+———-+
1 row in set (0.00 sec)

2.比较运算符

(1).一些基础的运算符=,<,>,<=,>=,!=(<>这个也是不等于的意思)
这里成立返回1,不成立返回0

select 2<3,2>3,2=3,2<=3,2>=3,2!=3,2<>3;
运算结果
+—–+—–+—–+——+——+——+——+
| 2<3 | 2>3 | 2=3 | 2<=3 | 2>=3 | 2!=3 | 2<>3 |
+—–+—–+—–+——+——+——+——+
| 1 | 0 | 0 | 1 | 0 | 1 | 1 |
+—–+—–+—–+——+——+——+——+
1 row in set (0.00 sec)

(2)is null是为空吗,is not null 是不为空吗;

select 1 is null,2 is not null,null is null,null is not null;
运算结果
+———–+—————+————–+——————+
| 1 is null | 2 is not null | null is null | null is not null |
+———–+—————+————–+——————+
| 0 | 1 | 1 | 0 |
+———–+—————+————–+——————+
1 row in set (0.00 sec)

(3).between … and …介于什么和什么之间,not between… and…不介于什么什么

select 1 between 1 and 10,1 not between 1 and 10;//1是否介于1和10之间
,1是否不介于1和10之间
运算结果
+——————–+————————+
| 1 between 1 and 10 | 1 not between 1 and 10 |
+——————–+————————+
| 1 | 0 |
+——————–+————————+
1 row in set (0.00 sec)

(4).in…属于什么什么,not in…不属于什么什么

select 3 in (1,2,3,4,5),7 not in (1,2,3,7);
运算结果
+——————+——————–+
| 3 in (1,2,3,4,5) | 7 not in (1,2,3,7) |
+——————+——————–+
| 1 | 0 |
+——————+——————–+
1 row in set (0.00 sec)

(5).like (“str%”)匹配str吗,not like (“str%”)不匹配str吗

select "abcdefg" like ("abc%"),"abcdefg" not like ("abc%");
运算结果
+————————-+—————————–+
| "abcdefg" like ("abc%") | "abcdefg" not like ("abc%") |
+————————-+—————————–+
| 1 | 0 |
+————————-+—————————–+
1 row in set (0.00 sec)

(6).regexp正则表达式"^“表示匹配开始部分吗,”$"表示匹配结尾部分

select "abcdef" regexp "^ap","abcdef" regexp "ef$";
运算结果
+———————–+———————–+
| "abcdef" regexp "^ap" | "abcdef" regexp "ef$" |
+———————–+———————–+
| 0 | 1 |
+———————–+———————–+
1 row in set (0.00 sec)

3.逻辑运算符

(1).&&逻辑与全1为1,有0则0(有NULL则为NULL)

select null && 1,2 && 2, 3 && 0;
运算结果
+———–+——–+——–+
| null && 1 | 2 && 2 | 3 && 0 |
+———–+——–+——–+
| NULL | 1 | 0 |
+———–+——–+——–+
1 row in set (0.00 sec)

(2).||逻辑或有1为1,全0为0

select null || 1,2 || 2, 3 || 0;
运算结果
+———–+——–+——–+
| null || 1 | 2 || 2 | 3 || 0 |
+———–+——–+——–+
| 1 | 1 | 1 |
+———–+——–+——–+
1 row in set (0.00 sec)

(3).!逻辑非0为1,1为0(有NULL则为NULL)

select !null,!1,!0;
运算结果
+——-+—-+—-+
| !null | !1 | !0 |
+——-+—-+—-+
| NULL | 0 | 1 |
+——-+—-+—-+
1 row in set (0.00 sec)

(4).xor异或“一真一假则为1,全真全假则为0”(有NULL则为NULL)

select null XOR 1,2 XOR 2, 3 XOR 0;
运算结果
+————+———+———+
| null XOR 1 | 2 XOR 2 | 3 XOR 0 |
+————+———+———+
| NULL | 0 | 1 |
+————+———+———+
1 row in set (0.00 sec)

4.位运算符(将其转换为二进制数后对应进行上述3运算得到

一个新的二进制数后转化为十进制数)

(1)&按位与

select 3&2;
运算结果
+—–+
| 3&2 |
+—–+
| 2 |
+—–+
1 row in set (0.00 sec)

(2).|按位或

select 5|6;
运算结果
+—–+
| 5|6 |
+—–+
| 7 |
+—–+
1 row in set (0.00 sec)

(3)~按位取反

select ~3;
运算结果
+———————-+
| ~3 |
+———————-+
| 18446744073709551612 |
+———————-+
1 row in set (0.00 sec)

(4).^按位异或

select 3^5;
运算结果
+—–+
| 3^5 |
+—–+
| 6 |
+—–+
1 row in set (0.00 sec)

(5).<<按位左移(3向左移两位)

select 3<<2;
运算结果
+——+
| 3<<2 |
+——+
| 12 |
+——+
1 row in set (0.00 sec)

(6).>>按位右移(3向右移2位)

select 3>>2;
运算结果
+——+
| 3>>2 |
+——+
| 0 |
+——+
1 row in set (0.00 sec)

5.表达式和运算符的优先级

以下是MySQL中运算符的优先级,从高到低排列:

  • !(逻辑非)、BINARY、COLLATE

  • -(一元负号)、~(按位取反)

  • ^(按位异或)

  • *(乘法)、/(除法)、DIV(整除)、% 或 MOD(取余)

  • +(加法)、-(减法)

  • <<(左移)、>>(右移)

  • &(按位与)

  • |(按位或)

  • =(比较)、<=>(安全等于)、>=、>、<=、<、<> 或 !=(不等于)、IS、LIKE、REGEXP、IN

  • BETWEEN、CASE、WHEN、THEN、ELSE

  • NOT

  • AND 或 &&

  • XOR

  • OR 或 ||

  • =(赋值)、:=

  • 赞(0)
    未经允许不得转载:网硕互联帮助中心 » MySQL的运算符与表达式(基础版,新手教学)
    分享到: 更多 (0)

    评论 抢沙发

    评论前必须登录!