SELECT last_name,salary,commission_pct FROM employees WHERE commission_pct ISNULL; #或 SELECT last_name,salary,commission_pct FROM employees WHERE ISNULL(commission_pct);
练习:查询表中commission_pct不为null的数据有哪些
SELECT last_name,salary,commission_pct FROM employees WHERE commission_pct ISNOTNULL; #或 SELECT last_name,salary,commission_pct FROM employees WHERENOT commission_pct <=>NULL;
LEAST() \ GREATEST
利用acsii码规则比较字母
mysql>SELECT LEAST('g','b','t','m'),GREATEST('g','b','t','m') ->FROM DUAL; +------------------------+---------------------------+ | LEAST('g','b','t','m') | GREATEST('g','b','t','m') | +------------------------+---------------------------+ | b | t | +------------------------+---------------------------+ 1rowinset (0.00 sec)
SELECT LEAST(first_name,last_name),LEAST(LENGTH(first_name),LENGTH(last_name)) FROM employees;
BETWEEN 条件下界1 AND 条件上界2 (查询条件1和条件2范围内的数据,包含边界)
查询工资在6000 到 8000的员工信息
SELECT employee_id,last_name,salary FROM employees #或 #where salary between6000and8000;#交换6000 和 8000之后,查询不到数据 WHERE salary >=6000&& salary <=8000;
查询工资不在6000 到 8000的员工信息
SELECT employee_id,last_name,salary FROM employees WHERE salary NOTBETWEEN6000AND8000;
in (set)\ not in (set) 离散查询
练习:查询部门为10,20,30部门的员工信息
SELECT last_name,salary,department_id FROM employees #where department_id =10or department_id =20or department_id =30; WHERE department_id IN (10,20,30);
查询工资不是6000,7000,8000的员工信息
SELECT last_name,salary,department_id FROM employees WHERE salary NOTIN (6000,7000,8000);
LIKE :模糊查询
% : 代表不确定个数的字符 (0个,1个,或多个)
#练习:查询last_name中包含字符'a'的员工信息 SELECT last_name FROM employees WHERE last_name LIKE'%a%';
#练习:查询last_name中包含字符'a'且包含字符'e'的员工信息 #写法1: SELECT last_name FROM employees WHERE last_name LIKE'%a%'AND last_name LIKE'%e%'; #写法2: SELECT last_name FROM employees WHERE last_name LIKE'%a%e%'OR last_name LIKE'%e%a%';
_ :代表一个不确定的字符
#练习:查询第3个字符是'a'的员工信息 SELECT last_name FROM employees WHERE last_name LIKE'__a%';
需要使用转义字符: \
#练习:查询第2个字符是_且第3个字符是'a'的员工信息 SELECT last_name FROM employees WHERE last_name LIKE'_\_a%';
#或者 (了解)代表使用'$'符作为转义字符 SELECT last_name FROM employees WHERE last_name LIKE'_$_a%'ESCAPE'$';