基础查询
1.查询表中的单个字段
SELECT last_name FROM t_mysql_employees;
2.查询表中的多个字段
SELECT last_name,salary,email FROM t_mysql_employees;
3.查询表中的所有字段
SELECT * FROM t_mysql_employees;
4.查询常量值
SELECT 100;
SELECT 'john';
5.查询表达式
SELECT 100%98;
6.查询函数
SELECT VERSION();
7.起别名
①使用as
②空格
8.去重
案例:查询员工表中涉及到的所有的部门编号
SELECT DISTINCT department_id FROM t_mysql_employees;
9.+号的作用
java中的+号:
①运算符,两个操作数都为数值型
②连接符,只要有一个操作数为字符串
mysql中的+号:
仅仅只有一个功能:运算符
过滤查询案例
一、按条件表达式筛选
案例1:查询工资>12000的员工信息
SELECT * FROM t_mysql_employees WHERE salary>12000;
案例2:查询部门编号不等于90号的员工名和部门编号
SELECT last_name ,department_id FROM t_mysql_employee WHERE department_id<>90;
二、按逻辑表达式筛选
案例1:查询工资z在10000到20000之间的员工名、工资以及奖金
SELECT last_name,salary,commission_pct FROM t_mysql_employees WHERE salary>=10000 AND salary<=20000;
案例2:查询部门编号不是在90到110之间,或者工资高于15000的员工信息
SELECT * FROM t_mysql_employees WHERE NOT(department_id>=90 AND department_id<=110) OR salary>15000;
三、模糊查询
1.like
案例1:查询员工名中包含字符a的员工信息
select * from employees where last_name like '%a%';
案例2:查询员工名中第三个字符为e,第五个字符为a的员工名和工资
select last_name,salary FROM t_mysql_employees WHERE last_name LIKE '__e_a%';
案例3:查询员工名中第二个字符为_的员工名
SELECT last_name FROM t_mysql_employees WHERE last_name LIKE '_$_%' ESCAPE '$';
2.between and
案例1:查询员工编号在100到120之间的员工信息
SELECT * FROM t_mysql_employees WHERE employee_id BETWEEN 100 AND 120;
3.in
案例:查询员工的工种编号是 IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号
SELECT last_name,job_id FROM t_mysql_employees WHERE job_id IN( 'IT_PROT' ,'AD_VP','AD_PRES');
4、is null
案例1:查询没有奖金的员工名和奖金率
SELECT last_name,commission_pct FROM t_mysql_employees WHERE commission_pct IS NULL;
案例2:查询有奖金的员工名和奖金率
SELECT last_name,commission_pct FROM t_mysql_employees WHERE commission_pct IS NOT NULL;
安全等于 <=>
案例1:查询没有奖金的员工名和奖金率
SELECT last_name,commission_pct FROM t_mysql_employees WHERE commission_pct <=>NULL;
案例2:查询工资为12000的员工信息
SELECT last_name,salary FROM t_mysql_employees WHERE salary <=> 12000;
is null pk <=>
IS NULL:仅仅可以判断NULL值,可读性较高,建议使用
<=> :既可以判断NULL值,又可以判断普通的数值,可读性较低
order by 子句
1、按单个字段排序
案例:按员工表薪资排序
SELECT * FROM t_mysql_employees ORDER BY salary DESC;
2、添加筛选条件再排序
案例:查询部门编号>=90的员工信息,并按员工编号降序
SELECT * FROM t_mysql_employees WHERE department_id>=90 ORDER BY employee_id DESC;
3、按表达式排序
案例:查询员工信息 按年薪降序
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) FROM t_mysql_employees ORDER BY salary*12*(1+IFNULL(commission_pct,0)) DESC;
4、按别名排序
案例:查询员工信息 按年薪升序
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) 年薪 FROM t_mysql_employees
5、按函数排序
案例:查询员工名,并且按名字的长度降序
SELECT LENGTH(last_name),last_name FROM t_mysql_employees ORDER BY LENGTH(last_name) DESC;
6、按多个字段排序
案例:查询员工信息,要求先按工资降序,再按employee_id升序
SELECT * FROM t_mysql_employees ORDER BY salary DESC,employee_id ASC;
排序练习
1.查询员工的姓名和部门号和年薪,按年薪降序 按姓名升序
select last_name,department_id,salary*12*(1+IFNULL(commission_pct,0)) from t_mysql_employees ORDER BY salary*12*(1+IFNULL(commission_pct,0)) desc,last_name asc
2.选择工资不在8000到17000的员工的姓名和工资,按工资降序
SELECT last_name,salary from t_mysql_employees ORDER BY salary*12*(1+IFNULL(commission_pct,0)) desc,salary not in(8000,17000)
3.查询邮箱中包含e的员工信息,并先按邮箱的字节数降序,再按部门号升序
SELECT * from t_mysql_employees where email like '%e%' ORDER BY LENGTH(email) desc ,department_id asc