# 查询员工编号(empno)为7788的员工姓名(ename)和所在部门的名称(dname)。
select ename,dname from emp,dept where emp.deptno = dept.deptno;
# 内连接
select * from dept
inner join emp
on emp.deptno = dept.deptno;
select * from emp
inner join dept
using(deptno); #通用列 不通用
#外连接
select * from emp
left join dept
on emp.deptno = dept.deptno;
select * from dept
left join emp
on emp.deptno = dept.deptno;
#等值连接
#自然连接肯定是等值连接
select * from emp NATURAL join dept;
#查询所有员工和他的上级领导的姓名。
select e1.ename emp,e2.ename mgr from emp e1
left join emp e2
on e1.mgr = e2.empno;
#1.查询平均工资最高的部门的部门名称和平均工资。
#1.1 连接 平均工资
select dname,avg(sal) from emp,dept
where emp.deptno = dept.deptno
group by emp.deptno
order by avg(sal) desc
limit 0,1;
#1.2 平均工资最高部门 求部门名称
select dname,e.avg from dept,
(select deptno,avg(sal) avg from emp
group by deptno
order by avg desc
limit 0,1) e
where dept.deptno = e.deptno;
#2.查询工资>2000且不在20号部门的员工的姓名和所在部门信息
select * from emp where sal > 2000 and deptno <> 20;
#3.查询薪水大于scott的员工信息和所在部门的信息。
#3.1 scott薪水
select sal from emp where ename = 'scott';
# 3.2 大于scott薪水的员工信息
select * from emp where sal > (select sal from emp where ename = 'scott');
#查询薪水和20号部门员工相等不在20号部门员工信息。
select * from emp where sal =any(
select distinct sal from emp where deptno = 20) and deptno <>20;
#查询20号部门除了工资最高员工的员工信息。
select * from emp where sal >any(
select sal from emp where deptno = 20)
and deptno =20;
#4.查询和scott在同一部门的所有员工信息。
select * from emp where deptno = (select deptno from emp where ename = 'scott');
#5.查询大于20号部门的平均工资的20号部门的员工信息
select * from emp where sal >
(select avg(sal) from emp where deptno = 20)
and deptno = 20;
#6.查询大于所在部门平均工资的员工信息。
select emp.* from emp,
(select deptno,avg(sal) avg from emp
group by deptno) e
where emp.deptno = e.deptno and sal > e.avg;
select * from emp e where sal > (
select avg(sal) from emp e1 where e1.deptno = e.deptno
);
#1.主查询遍历整个emp表
#2.主查询读取某一条记录的deptno值,将该值交给子查询
#3.子查询根据主查询的传来的deptno值,查询出指定部门的平均工资然后将整体结果返回给主查询
#4.主查询根据子查询的结果最终执行。
# 7.查询工资>20号部门的所有的员工信息。
select * from emp where sal >(
select max(sal) from emp where deptno =20);
select * from emp where sal >all(select distinct sal from emp where deptno = 20);
#8.薪水>2000的员工所在部门的信息。
#关联
select dept.* from emp,dept
where emp.deptno = dept.deptno and sal >2000;
#子查询
select * from dept where deptno in (
select distinct deptno from emp where sal > 2000);
#exists: in/=/>/<主查询条件字段和子查询的返回结果字段必须一一对应
select * from dept where exists
(select * from emp where sal > 2000 and dept.deptno = emp.deptno);
#in和exists区别
#in 先执行子查询,将结果返回给主查询,主查询继续执行。
#exists 先执行主查询,将主查询的值依次在子查询中进行匹配,根据是否匹配返回true或者false,如果true值连接展示否则不展示。
# 多表和子查询
# 子查询 --> 查询条件和结果放在一张表。
#结果分布于多张表
# 关联查询
# 查询20号部门以及工资>2000的员工信息
select * from emp where deptno = 20 or sal > 2000;
select * from emp where deptno = 20
union
select * from emp where sal > 2000;
#张三 --> 100 -->李四
update account set money = money - 100 where name = 'zs';
update account set money = money + 100 where name = 'ls';
# 查看mysql的事务自动提交
show variables like 'autocommit';
#修改自动提交
set autocommit = 0;
#显式开启事务(begin)
start transaction;
#在同一个事务
update account set money = money + 100 where name = 'zs';
update account set money = money - 100 where name = 'ls';
#手动提交或回滚事务
#commit;
rollback;
#开启自动提交
set autocommit = 1;
#查看事务隔离级别
SELECT @@tx_isolation
#修改
set session transaction isolation level
#存储过程
#服务器端运行的可重复调用的sql代码块,包含名称,输入输出参数,一组sql
#创建存储过程
#查询所有的用户名称和部门名称
#创建
delimiter //;
create procedure sel_emp()
begin
select dname,ename from emp,dept where dept.deptno=emp.deptno;
end;
#调用
call sel_emp();
#参数的传入
# 根据部门编号查询员工
delimiter //;
create procedure findEmpByNo(dno int)
begin
select * from emp where deptno = dno;
end;
call findEmpByNo(20);
#根据员工编号查询员工姓名
delimiter //;
create procedure findNameByNo(eno int,out v_name varchar(20))
begin
select ename into v_name from emp where empno = eno;
end;
call findNameByNo(7369,@v_name);
select @v_name;
#根据员工姓名查询员工职位
delimiter //;
create procedure findJob(inout name_job varchar(20))
begin
select job into name_job from emp where ename = name_job;
end;
set @name_job='smith';
call findJob(@name_job);
select @name_job;
#if(成绩分级)
delimiter //;
create procedure score_level(score int)
begin
#变量声明
declare v_level varchar(20);
if score >= 80 then
#变量赋值
set v_level='A';
elseif score>=60 then
set v_level='B';
else
set v_level='C';
end if;
select v_level;
end;
call score_level(80);
#循环(1+2+...+100)
delimiter //;
create procedure calc()
begin
declare i int;
declare sum int;
set i=1;
set sum=0;
while i<=100 do
set sum = sum + i;
set i = i + 1;
end while;
select sum;
end;
call calc();
delimiter //;
create procedure calc1()
begin
declare i int;
declare sum int;
set i=1;
set sum=0;
lip:loop
set sum = sum + i;
set i = i +1;
if i>100 then
leave lip;
end if;
end loop;
select sum;
end;
call calc1();
delimiter //;
create procedure calc2()
begin
declare i int;
declare sum int;
set i=1;
set sum=0;
repeat
set sum = sum + i;
set i = i + 1;
until i > 100
end repeat;
select sum;
end;
call calc2();
#存储函数(函数)
#存储在服务器端,有返回值,函数作为sql一部分使用。
#根据用户编号查询姓名
delimiter //;
create function findNameByNo(eno int)
returns varchar(20)
DETERMINISTIC
begin
declare v_name varchar(20);
select ename into v_name from emp where empno = eno;
return v_name;
end;
select findNameByNo(7788);
#编码
#乱码原因: utf-8
#client
#server
show variables like '%char%';
#修改编码为utf8 my.ini