mysql运算符大全及实例讲解

时间:2016-09-02
mysql和其他数据库及编程语言一样,也有运算符,包括算数运算符、比较运算符、逻辑运算符、位运算符等,本文章向大家介绍mysql运算符以及各种运算符的使用实例,需要的朋友可以参考一下。

mysql运算符如下表:

Name 描述
BETWEEN ... AND ... 判断一个值是否在指定的范围内
COALESCE() 返回参数中的第一个非空表达式
= 等于运算符
<=> NULL-safe equal to operator
> 大于运算符
>= 大于或等于运算符
GREATEST() 获取参数中最大的值
IN() 判断某个值是否在给定的序列里面
INTERVAL() Return the index of the argument that is less than the first argument
IS bool比较
IS NOT bool比较
IS NOT NULL 判断是否为空
IS NULL 判断是否为空
ISNULL() 判断参数是否为空
LEAST() 获取最小的值
< 小于运算符
<= 小于或等于运算符
LIKE like通配符
NOT BETWEEN ... AND ... 不在指定的范围
!=<> 不等于
NOT IN() 不在指定的序列里面
NOT LIKE like通配符
STRCMP() 比较两个字符串

 

运算符实例:

(1) =运算符:

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

(a, b) = (x, y)等价于(a = x) AND (b = y)

(2)<=>运算符

mysql> SELECT 1 <=> 1, NULL <=> NULL, 1 <=> NULL;
        -> 1, 1, 0
mysql> SELECT 1 = 1, NULL = NULL, 1 = NULL;
        -> 1, NULL, NULL

(a, b) <=> (x, y)等价于(a <=> x) AND (b <=> y))

(3)<>, !=不等于

mysql> SELECT '.01' <> '0.01';
        -> 1
mysql> SELECT .01 <> '0.01';
        -> 0
mysql> SELECT 'zapp' <> 'zappp';
        -> 1

(a, b) <> (x, y)等价于(a <> x) or (b <> y))

(4)<=小于或等于

mysql> SELECT 0.1 <= 2;
        -> 1

(a, b) <= (x, y)等价于(a < x) OR ((a = x) AND (b <= y))

(5)<小于运算符

mysql> SELECT 2 < 2;
        -> 0

(a, b) < (x, y)等价于(a < x) OR ((a = x) AND (b < y))

(6)>=大于或等于运算符

mysql> SELECT 2 >= 2;
        -> 1

(a, b) >= (x, y)等价于(a > x) OR ((a = x) AND (b >= y))

(7)is和is not运算符

mysql> SELECT 1 IS TRUE, 0 IS FALSE, NULL IS UNKNOWN;
        -> 1, 1, 1

mysql> SELECT 1 IS NOT UNKNOWN, 0 IS NOT UNKNOWN, NULL IS NOT UNKNOWN;
        -> 1, 1, 0

(8)is null 和is not null

mysql> SELECT 1 IS NULL, 0 IS NULL, NULL IS NULL;
        -> 0, 0, 1

SELECT * FROM tbl_name WHERE auto_col IS NULL
/*  http://www.manongjc.com/article/1435.html */
mysql> SELECT 1 IS NOT NULL, 0 IS NOT NULL, NULL IS NOT NULL;
        -> 1, 1, 0

(9)BETWEEN AND和 NOT BETWEEN AND

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

(10)COALESCE()

mysql> SELECT COALESCE(NULL,1);
        -> 1
mysql> SELECT COALESCE(NULL,NULL,NULL);
        -> NULL

(11)GREATEST()

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'

(12)in与not in 

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

(12)ISNULL()

mysql> SELECT ISNULL(1+1);
        -> 0
mysql> SELECT ISNULL(1/0);
        -> 1

(13)INTERVAL()

mysql> SELECT INTERVAL(23, 1, 15, 17, 30, 44, 200);
        -> 3
mysql> SELECT INTERVAL(10, 1, 10, 100, 1000);
        -> 2
mysql> SELECT INTERVAL(22, 23, 30, 44, 200);
        -> 0

(14)LEAST()

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'

上一页 下一页

原文地址:http://www.manongjc.com/article/1435.html