一、数据查询
1.1基本查询
语法:
select 列名 from 表名
关键字:
select 指定要查询的列
from 指定要查询的表
1.1.1 查询部分列
#查询员工表中所有员工的编号,名称
select empid,name from t_emp;
1.1.2 查询全部列
#查询员工表中全部的数据
select 全部列名 from t_emp;
#或者
select * from t_emp;
注意:在生产环境中,优先使用列名查询。*的方式需转换成全部列名,效率低,可读性差。
1.1.3对列中的数据进行运算
#查询员工中员工编号、名字、年薪
select emp_id,name,salary*12 from t_emp
算数运算符
+ 两列做加法运算
- 两列做减法运算
* 两列做乘法运算
/ 两列做除法运算
注意:%为占位符,而非模运算符
1.1.4 列的别名
语法:列 as '列名'
#查询员工中员工编号、名字、年薪(列名均为中文)
select emp_id as'员工编号',name as '名字',salary*12 '年薪' from t_emp
1.1.5查询结果去重
#查询员工表中所有经理的ID
select destinct manager_id from t_emp;
1.2排序查询
语法:select 列名 from 表名 order by 排序列[排序规则]
注:排序规则定义
ASC 对前面排序列做升序排序 (如不写排序规则默认为升序排列)
DESC 对前面排序列做降序排序
1.2.1依据单列排序
#查询员工编号、名字、薪资,按照薪资高低进行降序排列
select emp_id,name,salary from t_emp order by salary desc;
#查询员工编号、名字、薪资,按照薪资高低进行升序排列
select emp_id,name,salary from t_emp order by salary asc;//asc可不写
1.2.2依据多列排序
#查询员工编号、姓名、工资,按照工资高低降序排序,薪资相同时按编号升序排序
select emp_id,name.salary from t_emp order by salary desc,emp_id asc;
1.3条件查询
语法:select 列名 from 表名 where 条件;
关键字:where 在查询结果中,筛选符合条件的查询结果,条件为布尔表达式。
1.3.1 等值判断(=)
#查询薪资是11000的员工信息
select emp_id,name from t_emp where salary=11000;
1.3.2 逻辑判断(and,or,not)
#查询薪资是11000并且提成是0.30的员工信息
select emp_id,name from t_emp where salary=11000 and commission_pct = 0.30;
#查询薪资是11000或者薪资是8000的员工信息
select emp_id,name from t_emp where salary=11000 orsalary=8000;
#查询薪资是11000不等于8000的员工信息
select emp_id,name from t_emp where not salary=8000;
1.3.3不等值判断
#查询员工信息在6000-10000之间的员工信息
select emp_id,name,salary from t_emp wherer salary >=6000 and salary <=10000;
1.3.4区间判断
#查询员工的薪资在6000-10000之间的员工信息
select emp_id,name,salary from Tt_emp where salary between 6000 and 10000;
1.3.5 NULL值判断
语法: 列名 is null;
列名 is not null;
#查询没有提成的员工信息
select emp_id,name,salary from t_emp where commission_pct is null;
1.3.6枚举查询
#查询部门编号为70、80、90的员工信息
select emp_id,name,salary from t_emp where department_id in(70,80,90);
1.3.7模糊查询