**基础查询**
Select 字段名 from 表名;//查询整个表时,字段名换为 *
例:查询员工表中员工的姓名
select last_name from employees;
查询员工表中所有信息
select * from employees;
**条件查询**
select 字段名 from 表名 where 条件;
//条件运算符 >、<、>=、<=、=、!=、<>、<=>安全等于
//逻辑表达式 not,or,and (优先级 not>and>or)
例:查询月薪大于5000的员工信息
select * from employees where salary=5000;
**模糊查询**
1.like 搭配字符使用 “%”匹配多个字符,“_”匹配一个字符
例:查询名字第二个字母为e的员工信息
select * from employees where last_name like '_e%';
2.between....and....( 包含两个临界值且两个临界值不能颠倒)
例:查询工资在5000到12000的员工信息
select * from employees where salary between 5000 and 12000;
3.in 判断某个字段的值是否在某个列表里
例:查询员工ID为100和101的员工信息
select * from employees where id in (100,101);
4.is null/is not null (为空/不为空)
例:查询有奖金的员工
select * from employees where commission_pct is not null;
**排序查询**
order by 排序的字段名 asc/desc(asc升序/ desc降序)
例:查询员工的信息,按工资降序
select * from employees order by salary desc;
**分组函数与查询**
group by 分组的字段名;
max()//最大
min()//最小
sum()//求和
avg()//求平均
count()//计算非空的个数
以上函数都忽略空值
**分页查询**
limit 查询的起始条数(默认0开始),要查询的总条数
例:查询员工表第5条到第20条信息
select * from employees limit 4,16;
select.....from......where......group by.....having.....order by
关键字的语法顺序(不能颠倒)
例:查询员工姓名中包含字符a,每个部门的最低工资高于3000的部门编号,按工资降序,显示第5到第20条信息
select department_id,min(salary) 最低工资
from employees
where last_name like '%a%'
group by department_id
having min(salary)>3000
order by salary desc
limit 4,16;
MySQL常用查询命令
最新推荐文章于 2023-10-24 17:25:47 发布