注:此笔记有why的地方是我没有完全理解的地方!
算术运算符
+
-
*
/
DIV
%
MOD
SELECT 0.1 + 0.123, 0.1 - 0.123, 0.1 * 0.123;
SELECT 13/2, 13 DIV 2;
SELECT 10 % 3, 10 MOD 3, MOD(10,3);
SELECT 1/0, 100%0; 除法运算和模运算,如果除数为0,将是非法除数,返回结果为null
比较运算符
=
<> !=
<=>
<
<=
>
>=
between
in
is null
is not null
like
regexp rlike
= 相等返回1,否则返回0;null不能用=比较
SELECT 1=0,1=1,NULL=NULL;
SELECT 'a'=2; why?
<> 不等返回1,否则返回0;null不能用<>比较
SELECT 1<>0,1<>1,NULL<>NULL;
SELECT 'a'<>2; why?
<=> 相等返回1,否则返回0;null也可以正确比较
SELECT 1<=>0,1<=>1,NULL<=>NULL,NULL<=>1;
SELECT 'a'<=>2; why?
< 左侧小于右侧,返回1,否则返回0
SELECT 1<0, 1<1, 1<2, NULL<NULL,NULL<1;
SELECT 'b'<'c', 'b'<'b', 'b'<'a', 'b'<'z', 'a'<NULL;
SELECT 'a'<2; why?
SELECT '2'<2; why?
SELECT 'abc'<'ac'; why?
SELECT 'abc'<'a2'; why?
a between min and max ; a大于等于min并且小于等于max,返回1,否则返回0;
SELECT 1 BETWEEN -1 AND 5, 5 BETWEEN 2 AND 4, 6 BETWEEN 6 AND 9;
in a in (value1,value2...) a的值存在于列表中时,返回的值为1,否则返回0
SELECT 1 IN(1,2,3), 0 IN(1,2,3), 'b' IN(1,2,3,'b');
SELECT 0 IN(1,2,3,'b');
is null 若为null,返回值为1,否则返回0;
SELECT 0 IS NULL, NULL IS NULL;
is not null 返回值为1,否则为0
like a like "%1234%" 当a中含有字符串‘1234’时,返回1,否则0
SELECT 123456 LIKE '123%', 123456 LIKE '%123%', 123456 LIKE '%321%';
regexp str regexp str_pat 当str字符串含有str_pat相匹配的字符串时,返回1,否则0;
SELECT 123456 REGEXP 12, 'abcdefghikasdf' REGEXP 'abcd';
逻辑运算符
not ! 非
and && 与
or || 或
xor 异或
SELECT NOT 1, NOT 0, ! 12122 ,! NULL; not null的返回值为null
SELECT NOT 'sdfs'; why?
SELECT NOT '2';
SELECT 1 AND 2, 1 AND 0, '1' AND 1, '2' AND 1, 'e' AND 1, '0' AND 1; why?
SELECT 1 AND NULL; 操作数的任何一个为null,返回值为null
xor 逻辑异或 ,当任意一个操作数为null,返回值为null;
SELECT 1 XOR 1, 1 XOR 0, 0 XOR 0, NULL XOR 1;
位运算符
&
|
^
~
>>
<<