MySQL查询教程
查询Syntax:
SELECT
[* | DISTINCT | col_name | col_name as new_name ]
FROM table
WHERE condition
GROUP BY {col_name} HAVING where_condition
ORDER BY {col_name } [ASC | DESC]
LIMIT {[offset,] row_count | row_count OFFSET offset}
。select * from ..
select * from emp; 会查询所有的记录行;生产中杜绝使用(select * from emp;会做全表扫描)
select * from emp where empno='7900'; 会取出所有的字段;生产中杜绝使用
原因1:占用更多的内存空间(缓冲池innodb buffer pool)
原因2:查询条件是普通索引,就会进行回表查询(随机io,查询性能降低)
。select col_name from ..
select ename, job from emp where empno='7900';
索引覆盖(using index)
。select col_name as new_col_name from ..
select sum(sal) as t_sal from emp; 取别名
。select distinct col_name from ..
select distinct job from emp; 去重
where子句
。作用:用于条件过滤,有条件的从表中获取数据
。结构:where col_name 操作符 值
where name = 'ergou';
。where子句中的操作符
常用操作符 | |
---|---|
< , <= , > , >= , = , != | |
between..and.. | |
in(set) | |
like | |
not like | |
is null | |
and | |
or | |
not |
范围操作符:< , <= , > , >= , between..and..,in(set),not in(set)
select * from emp where empno > 7900;
select * from emp where empno >= 7900;
select * from emp where empno between 7900 and 9999; 查询结果包含两边的值
select * from emp where empno in(7900,7902);
select * from emp where empno not in(7900,7902); 在生产中杜绝使用;
等值查询:=
select * from emp where ename = 'smith';
注意事项:
字符串类型、日期类型的sql语句where条件中,一定要加引号,否则将会发生隐式转换,将进行全表扫描,效率极低。
explain select * from emp where ename = '007'; 使用到索引,索引应用级别为type:ref
explain select * from emp where ename = 007;没有使用到索引,将会进行全表扫描,sql效率非常低
不等值查询:!=
生产中不建议使用;如果是普通索引作为查询条件,将进行全表扫描;
select * from emp where empno != 7900;
模糊查询:like,not like
select * from emp where ename like 's%';
explain select * from emp where ename like '%n'; 生产中杜绝使用左边模糊查询
select * from emp where mgr is null;
逻辑操作符:and or not
select ename from emp where empno > 7900 and job= 'clerk';
select ename from emp where empno=7934 or empno=9999;
。where子句查询案例
查询工资高于2000的员工
select * from emp where sal > 2000;
查询工资在2000到3500的员工情况
select * from emp where sal between 2000 and 3500;
查询员工姓名首字母为S的员工姓名和工资
select ename ,sal from emp where ename like 's%';
查询empno为 7839,7902,7934的员工情况
select * from emp where empno in(7839,7902,7934);
查询没有上级的员工情况
select * from emp where mgr is null;
查询工资高于500或是岗位为MANAGER,同时还要满足他们的姓名首写字母为J的员工信息
select * from emp where (sal > 500 or job = 'MANAGER') and ename like 'j%';
group by .. having子句
。作用:对查询结果进行分组统计,having 用于限制分组结果的显示
。结构:group by col_name1, col_name2 having ..
。注意事项:select @@sql_mode; ONLY_FULL_GROUP_BY
查询列必须是group by后面的字段或者是聚合函数,出现其他列将报错
。group by 子句查询案例
查询每个部门的平均工资和最高工资
select deptno,avg(sal), max(sal) from emp group by deptno;
查询每个部门的每种岗位的平均工资和最低工资
select deptno, job, avg(sal),min(sal) from emp group by deptno,job;
查询部门平均工资低于2000的部门号和平均工资
select deptno,avg(sal) from emp group by deptno having avg(sal) > 2000;
order by子句
。作用:用于排序
。结构:order by col_name [asc | desc];
select sal from emp order by sal ;
。注意事项:排序字段是索引,如果指定排序为降序,无法使用索引的排序,需要重新排序
。order by子句案例
查询工资的从低到高的顺序显示员工的信息
select * from emp order by sal asc;
查询按照员工的年工资进行降序排序显示员工的姓名和年工资
select ename , sal*12 as y_sal from emp order by y_sal desc;
limit子句
。作用:用于限制取出的记录数,也可以用于分页
select * from emp limit 2;
分页:limit (pagenow-1)*pagesize,pagesize
。分页查询案例:
按员工号升序查询员工姓名、工资, 每页显示3条记录。请分别显示 第一页,第二页,第三页
select ename,sal from emp order by empno asc limit 0,3;
select ename,sal from emp order by empno asc limit 3,3;
select ename,sal from emp order by empno asc limit 6,3;