一、操作符优先级

展示了所有操作符的执行优先级,从高到低,同一行中的操作符优先级相同,相同优先级的情况下则从左到右执行

如果想改变优先级执行顺序,则可以使用括号:

mysql> SELECT 1+2*3;  #7
mysql> SELECT (1+2)*3;#9

二、对比操作符

#对比操作符的执行结果为true,false,null三种
Between A and B 	#代表检查值是否在A和B之间
Coalesce()			#代表返回第一个非Null的值
=					#代表相等操作符
>					#代表大于操作符
>=					#代表大于等于操作符
Greatest()			#代表返回最大的值
In()				#代表检查值是否在一系列的值之中
Interval()			#代表返回比第一个参数小的参数的位置
is/is not			#代表检查值是否与布尔值相同/不同
Is not null			#代表检查值是否是非NULL
Is null				#代表检查值是否是NULL
Isnull()			#代表检查参数是NULL
Least()				#代表返回最小的参数
<					#代表小于操作符
<=					#代表小于等于操作符
Like				#代表字符匹配
Not between A and B	#代表检查值是否不在A和B的范围之内
!=/<>				#代表不等于操作符
Not in()			#代表检查值是否不在一系列值的当中
Not like			#代表检查值是否不匹配
Strcmp()			#对比两个字符串
-----------------------------------------------------------------------------------------
#=号对比操作符
mysql> SELECT 1 = 0;  #0
mysql> SELECT '0' = 0; #1
mysql> SELECT '0.0' = 0;  #1
mysql> SELECT '0.01' = 0; #0
mysql> SELECT '.01' = 0.01; #1


#<>/!=号对比操作符
mysql> SELECT '.01' <> '0.01'; #1
mysql> SELECT .01 <> '0.01';  #0
mysql> SELECT 'zapp' <> 'zappp'; #1


#<=小于等于对比操作符
mysql> SELECT 0.1 <= 2;  #1

#<小于对比操作符
mysql> SELECT 2 < 2;   #0

#>=大于等于对比操作符
mysql> SELECT 2 >= 2; #1

#>大于对比操作符
mysql> SELECT 2 > 2; #0


#is操作符
mysql> SELECT 1 IS TRUE, 0 IS FALSE, NULL IS UNKNOWN;  #1, 1, 1

#Is not操作符
mysql> SELECT 1 IS NOT UNKNOWN, 0 IS NOT UNKNOWN, NULL IS NOT UNKNOWN;  #1, 1, 0

#Is null对比操作符
mysql> SELECT 1 IS NULL, 0 IS NULL, NULL IS NULL; #0, 0, 1

#Is not null对比操作符
mysql> SELECT 1 IS NOT NULL, 0 IS NOT NULL, NULL IS NOT NULL;  #1, 1, 0

#Expr between min and max对比操作符
#相当于min <= expr AND expr <= max
mysql> SELECT 2 BETWEEN 1 AND 3, 2 BETWEEN 3 and 1;  #1, 0
mysql> SELECT 1 BETWEEN 2 AND 3; #0
mysql> SELECT 'b' BETWEEN 'a' AND 'c'; #1
mysql> SELECT 2 BETWEEN 2 AND '3'; #1
mysql> SELECT 2 BETWEEN 2 AND 'x-3'; #0

#expr NOT BETWEEN min AND max
#相当于NOT (expr BETWEEN min AND max)

#COALESCE(value,...)对比操作符
#返回第一个非NULL的值,如果没有非null值,则返回NULL
mysql> SELECT COALESCE(NULL,2); #2
mysql> SELECT COALESCE(NULL,NULL,NULL); #NULL


#GREATEST(value1,value2,...)
#返回其中最大的值
mysql> SELECT GREATEST(2,0);  #2
mysql> SELECT GREATEST(34.0,3.0,5.0,767.0); #767.0
mysql> SELECT GREATEST('B','A','C'); #'C'



#expr IN (value,...)对比操作符
#当expr值能在values中找到,则返回1,否则返回0
mysql> SELECT 2 IN (0,3,5,7);  #0
mysql> SELECT 'wefwf' IN ('wee','wefwf','weg'); #1
mysql> SELECT (3,4) IN ((1,2), (3,4));  #1
mysql> SELECT (3,4) IN ((1,2), (3,5));  #0

#expr NOT IN (value,...)对比操作符


#Isnull(expr)操作符
#如果expr是null,则返回1,否则返回0
mysql> SELECT ISNULL(1+1);  #0
mysql> SELECT ISNULL(1/0);  #1

#LEAST(value1,value2,...)
#返回最小值,如果其中有值为null,则返回null
mysql> SELECT LEAST(2,0); #0
mysql> SELECT LEAST(34.0,3.0,5.0,767.0); #3.0
mysql> SELECT LEAST('B','A','C');  #'A'
01-18 09:11