6. 最小值运算符
语法格式为:LEAST(值1,值2,…,值n)。其中,“值n”表示参数列表中有n个值。在有两个或多个参数的情况下,返回最小值。
mysql> SELECT LEAST (1,0,2), LEAST('b','a','c'), LEAST(1,NULL,2); +---------------+--------------------+-----------------+ | LEAST (1,0,2) | LEAST('b','a','c') | LEAST(1,NULL,2) | +---------------+--------------------+-----------------+ | 0 | a | NULL | +---------------+--------------------+-----------------+ 1 row in set (0.00 sec)
#LEAST(value1,value2,...) 最小值 GREATEST(value1,value2,...) 最大值 SELECT LEAST('a','c','d','z','v'), GREATEST('a','c','d','z','v') FROM DUAL; SELECT LEAST(first_name,last_name) , LEAST(LENGTH(first_name),LENGTH(last_name)) FROM employees;
由结果可以看到,当参数是整数或者浮点数时,LEAST将返回其中最小的值;当参数为字符串时,返回字母表中顺序最靠前的字符;当比较值列表中有NULL时,不能判断大小,返回值为NULL。
7. 最大值运算符
语法格式为:GREATEST(值1,值2,…,值n)。其中,n表示参数列表中有n个值。当有两个或多个参数时,返回值为最大值。假如任意一个自变量为NULL,则GREATEST()的返回值为NULL。
mysql> SELECT GREATEST(1,0,2), GREATEST('b','a','c'), GREATEST(1,NULL,2); +-----------------+-----------------------+--------------------+ | GREATEST(1,0,2) | GREATEST('b','a','c') | GREATEST(1,NULL,2) | +-----------------+-----------------------+--------------------+ | 2 | c | NULL | +-----------------+-----------------------+--------------------+ 1 row in set (0.00 sec)
由结果可以看到,当参数中是整数或者浮点数时,GREATEST将返回其中最大的值;当参数为字符串时,返回字母表中顺序最靠后的字符;当比较值列表中有NULL时,不能判断大小,返回值为NULL。
#LEAST(value1,value2,...) 最小值 GREATEST(value1,value2,...) 最大值 SELECT LEAST('a','c','d','z','v'), GREATEST('a','c','d','z','v') FROM DUAL; SELECT LEAST(first_name,last_name) , LEAST(LENGTH(first_name),LENGTH(last_name)) FROM employees;
8. BETWEEN AND运算符
BETWEEN运算符使用的格式通常为SELECT D FROM TABLE WHERE C BETWEEN AAND B,此时,当C大于或等于A,并且C小于或等于B时,结果为1,否则结果为0。
mysql> SELECT 1 BETWEEN 0 AND 1, 10 BETWEEN 11 AND 12, 'b' BETWEEN 'a' AND 'c'; +-------------------+----------------------+-------------------------+ | 1 BETWEEN 0 AND 1 | 10 BETWEEN 11 AND 12 | 'b' BETWEEN 'a' AND 'c' | +-------------------+----------------------+-------------------------+ | 1 | 0 | 1 | +-------------------+----------------------+-------------------------+ 1 row in set (0.00 sec)
SELECT last_name, salary FROM employees WHERE salary BETWEEN 2500 AND 3500;
#BETWEENt 条件1 AND 条件2 (查询条件1到条件二范围的值 ,包含边界值 例如 条件2是小于 5000 那就包括 5000) #条件1必须为起始条件 条件2位终止条件 注意 不然查不到数据 #查询表employees 中salary的值为6000-8000的包含6000和8000 SELECT employee_id,last_name,salary FROM employees WHERE salary BETWEEN 6600 AND 8000; #或者下面 SELECT employee_id,last_name,salary FROM employees WHERE salary>=6000 && salary<=8000; #错误案例无法查询到数据 SELECT employee_id,last_name,salary FROM employees WHERE salary>=8000 && salary<=6000; #查询不在6000和8000之间的数据 SELECT employee_id,last_name,salary FROM employees WHERE salary NOT BETWEEN 6600 AND 8000; #or 或的意思 AND 和&& 为且的意思 SELECT employee_id,last_name,salary FROM employees WHERE salary<6000 or salary>8000;
9. IN运算符
IN运算符用于判断给定的值是否是IN列表中的一个值,如果是则返回1,否则返回0。如果给定的值为NULL,或者IN列表中存在NULL,则结果为NULL。
mysql> SELECT 'a' IN ('a','b','c'), 1 IN (2,3), NULL IN ('a','b'), 'a' IN ('a', NULL); +----------------------+------------+-------------------+--------------------+ | 'a' IN ('a','b','c') | 1 IN (2,3) | NULL IN ('a','b') | 'a' IN ('a', NULL) | +----------------------+------------+-------------------+--------------------+ | 1 | 0 | NULL | 1 | +----------------------+------------+-------------------+--------------------+
WHERE manager_id IN (100, 101, 201); • 1
#in (集合) not in (集合) #in 多条件值查询 查询 值为 10 20 30 SELECT department_id,last_name,salary FROM employees WHERE department_id =10 OR department_id=20 OR department_id=30 ; SELECT department_id,last_name,salary FROM employees WHERE department_id in(10,20,30); #查询不为 10 20 30 的 not in SELECT department_id,last_name,salary FROM employees WHERE department_id NOT in(10,20,30);
10. NOT IN运算符
NOT IN运算符用于判断给定的值是否不是IN列表中的一个值,如果不是IN列表中的一个值,则返回1,否则返回0。
mysql> SELECT 'a' NOT IN ('a','b','c'), 1 NOT IN (2,3); +--------------------------+----------------+ | 'a' NOT IN ('a','b','c') | 1 NOT IN (2,3) | +--------------------------+----------------+ | 0 | 1 | +--------------------------+----------------+ 1 row in set (0.00 sec)
#in (集合) not in (集合) #in 多条件值查询 查询 值为 10 20 30 SELECT department_id,last_name,salary FROM employees WHERE department_id =10 OR department_id=20 OR department_id=30 ; SELECT department_id,last_name,salary FROM employees WHERE department_id in(10,20,30); #查询不为 10 20 30 的 not in SELECT department_id,last_name,salary FROM employees WHERE department_id NOT in(10,20,30);