Day36 数据库的操作

 

视图操作:

1.左连接查询

select * from person left join dept on person.dept_id = dept.did

 2. 右连接

3. 内连接  inner join

 4. UNION ALL  全连接

 

=======================

一 、 多表操作

select * from person,dept

二 、多表联合操作 

 select * from person,dept where person.dept_id =dept.did   

select * from person,dept where person.dept_id =dept.did and person.salary >3000

三、多表连接查询

左连接查询 (以左表作为基准)

select *from person left JOIN dept  on person.dept_id = dept.did

 

内连接查询 

 select * from person inner JOIN dept where  person.dept_id =dept.did

 

4.全连接查询 full JOIN
select * from person LEFT JOIN dept ON person.dept_id = dept.did
UNION ALL
select * from person RIGHT JOIN dept on person.dept_id = dept.did;

二 、 复杂的多表查询  。 

1. 查询出 教学部 年龄大于20岁,并且工资小于40000的员工,按工资倒序排列.(要求:分别使用多表联合查询和内连接查询)

 select * from person inner join dept on person.dept_id = dept.did and dept.dname = '教学部' and person.age >20 and salary <40000 order by person.salary asc

2.查询每个部门中最高工资和最低工资是多少,显示部门名称

select dept.dname , MAX(salary),MIN(salary) from person LEFT JOIN dept on person.dept_id =dept.did group by person.dept_id

 2.求最大工资那个人的姓名和薪水

 

 1.求最大工资

select max(salary) from person;
2.求最大工资那个人叫什么
select name,salary from person where salary=53000;

合并
select name,salary from person where salary=(select max(salary) from person);

 3. 求工资高于所有人员平均工资的人

 
  
1.求平均工资
select avg(salary) from person;

2.工资大于平均工资的 人的姓名、工资
select name,salary from person where salary > 21298.625;

合并
select name,salary from person where salary >(select avg(salary) from person);

 

视图操作:

1.左连接查询

select * from person left join dept on person.dept_id = dept.did

 2. 右连接

3. 内连接  inner join

 4. UNION ALL  全连接

 

=======================

一 、 多表操作

select * from person,dept

二 、多表联合操作 

 select * from person,dept where person.dept_id =dept.did   

select * from person,dept where person.dept_id =dept.did and person.salary >3000

三、多表连接查询

左连接查询 (以左表作为基准)

select *from person left JOIN dept  on person.dept_id = dept.did

 

内连接查询 

 select * from person inner JOIN dept where  person.dept_id =dept.did

 

4.全连接查询 full JOIN
select * from person LEFT JOIN dept ON person.dept_id = dept.did
UNION ALL
select * from person RIGHT JOIN dept on person.dept_id = dept.did;

二 、 复杂的多表查询  。 

1. 查询出 教学部 年龄大于20岁,并且工资小于40000的员工,按工资倒序排列.(要求:分别使用多表联合查询和内连接查询)

 select * from person inner join dept on person.dept_id = dept.did and dept.dname = '教学部' and person.age >20 and salary <40000 order by person.salary asc

2.查询每个部门中最高工资和最低工资是多少,显示部门名称

select dept.dname , MAX(salary),MIN(salary) from person LEFT JOIN dept on person.dept_id =dept.did group by person.dept_id

 2.求最大工资那个人的姓名和薪水

 

 1.求最大工资

select max(salary) from person;
2.求最大工资那个人叫什么
select name,salary from person where salary=53000;

合并
select name,salary from person where salary=(select max(salary) from person);

 3. 求工资高于所有人员平均工资的人

 
1.求平均工资
select avg(salary) from person;

2.工资大于平均工资的 人的姓名、工资
select name,salary from person where salary > 21298.625;

合并
select name,salary from person where salary >(select avg(salary) from person);

 

猜你喜欢

转载自www.cnblogs.com/lw1095950124/p/10288281.html