day58
分组
参考:https://www.cnblogs.com/xp796/p/5262187.html
select dept, max(salary) from department group by dept;
从各个部门中挑出最高的薪水。
select dept, sum(salary) from department group by dept;
计算各个部门的总薪水。
select count(id), edlevel from department group by edlevel;
不同等级员工的数量。
select max(salary),dept from department group by dept having avg(salary)<3000;(条件)
从平均工资大于3000的部门中,挑出部门中最高的薪水。
连表操作
连表前两表有外键约束。
select * from course,teacher;
左右表都显示,但是两表之间联系并未体现。
select * from course,teacher where course.teacher_id = teacher.tid;
连表的第一种用法,teacher_id与tid对应合并。
# select * from teacher right join course on teacher.tid = course.teacher_id
# course右边全部显示(课的所有信息显示)
select * from teacher left join course on teacher.tid = course.teacher_id
select * from course left join teacher on teacher.tid = course.teacher_id
# teacher左边全部显示 (各个老师全部显示,尽管有些课没有该老师上课,没有课上的老师显示null)
select * from teacher innder join course on teacher.tid = course.teacher_id;
将出现null时一行隐藏
转储备份数据库
存在命令行当前路径下,mysqldump -u root db1 > db1.sql -p(含数据)
mysqldump -u root -d db1 > db1.sql -p(不含数据)
导入数据
create database db5; (首先该数据库必须先存在)
mysqldump -u root -d db5 < db1.sql -p;
实践:
导出电脑1数据
将数据导进电脑2