三、MySQL多表查询和子查询

2018-03-20  本文已影响0人  AKyS佐毅

1、隐式连接

 select concat(e.first_name,',',e.last_name),d.department_name
 from employees e,departments d where e.department_id = d.department_id;
select e.employee_id,e.first_name,d.department_id,d.department_name,d.location_id,l.city
from employees e,departments d,locations l
where e.department_id=d.department_id and d.location_id=l.location_id;
select e.first_name,e.salary,jg.grade_level
from employees e,job_grades jg
where e.salary between jg.lowest_sal and jg.higest_sal;

2、显式连接

-- 查询员工和其经理,如果没有经理,显示总经理,所有的员工必须显示
SELECT e.first_name, e.last_name, e.manager_id, IFNULL(m.first_name,'总经理')
from employees e LEFT JOIN employees m on e.manager_id = m.employee_id;

-- 查询last_name中有‘KI’的员工的经理的信息。
SELECT m.first_name ,m.last_name,e.last_name from employees e
 join employees m on e.last_name like '%KI%' and e.manager_id = m.employee_id;

-- 查询‘jones’部门同事姓名
SELECT e.last_name ,m.first_name ,m.last_name ,m.department_id,e.department_id 
from employees e JOIN employees m on e.department_id = m.department_id 
WHERE e.last_name = 'jones' and m.last_name <> 'jones';

3、子查询

--查询工资大于公司平均工资的员工
select first_name,last_name,salary from employees where salary > (select avg(salary) from employees);

4、DML加强

-- 1、备份employees表到employees_bck
CREATE TABLE employees_bak as SELECT * FROM employees;
-- 2、创建employees_bck2表,复制employees表结构
CREATE TABLE employees_bak2 as SELECT * FROM employees where 1=2;
-- 3、备份employees表中工资大于10000的员工信息;
CREATE TABLE employees_bak3 as SELECT * FROM employees where salary > 10000;
-- 4、使用employees表中的某几列创建employees_smallest表(id,fullname,hiredate,salary)。
CREATE TABLE employees_smallest as SELECT employee_id as id ,CONCAT(first_name,',',last_name) as fullname ,hire_date as hiredate, salary from employees;
   --1,修改114号员工的工资和205号员工相同
   update employees set salary = (select salary from employee where employee_id = 205) where employee_id = 114;
   --不能这样使用,在mysql中,子查询的表不能和要更新的表来自同一张表;
   --只能使用隐式连接来修改数据:
   update employees e1,employees e2 set e1.salary = e2.salary where e1.employee_id = 114 and e2.employee_id = 205;
   -- 2,将在牛津工作的员工的工资涨$1000
   update employees e,departments d,locations l set e.salary = e.salary+1000  where e.department_id = d.department_id and d.location_id = l.location_id and l.city='Oxford'
create table temp as select employee_id from ......;
delete from employees where employee_id in (select * from temp);

6、 查询的SQL语句

-- 查询last_name中有‘KI’的员工的经理的信息
SELECT m.first_name ,m.last_name,e.last_name from employees e join employees m on e.last_name like '%KI%' and e.manager_id = m.employee_id;

-- 查询出公司员工的last_name,department_name,city 
SELECT e.last_name,d.department_name ,l.city from employees e LEFT JOIN departments d  on d.department_id = e.department_id LEFT JOIN locations  l  on d.location_id = l.location_id;


-- 查询员工和其经理,如果没有经理,显示总经理
SELECT e.first_name, e.last_name, e.manager_id, IFNULL(m.first_name,'总经理')from employees e LEFT JOIN employees m on e.manager_id = m.employee_id;

-- 查询公司员工的工资等级
select e.first_name,e.salary,jg.grade_level from employees e,job_grades jg
where e.salary between jg.lowest_sal and jg.higest_sal;

--  查询公司员工的姓名和所属部门
SELECT e.first_name ,e .last_name , d.department_name from employees e LEFT join departments d on e.department_id = d.department_id;

SELECT e.first_name ,e.last_name FROM employees e WHERE e.department_id = 50;

-- 查询‘jones’部门同事姓名
SELECT e.last_name ,m.first_name ,m.last_name ,m.department_id,e.department_id from employees e JOIN employees m on e.department_id = m.department_id WHERE e.last_name = 'jones' and m.last_name <> 'jones';

SELECT min(salary) FROM employees GROUP BY department_id ;

-- 返回多行是错误的 不要用 = ,可以使用in
SELECT e.employee_id,e.last_name from employees e WHERE salary = (SELECT min(salary) FROM employees GROUP BY department_id);

SELECT department_id from employees WHERE last_name = 'jones';

--  查询‘jones’的部门同事姓名
SELECT first_name ,last_name , department_id from employees WHERE department_id = (SELECT department_id from employees WHERE last_name = 'jones') 
and last_name <> 'jones';

-- 查询公司工资最低的员工信息
SELECT e.first_name ,e.last_name ,e.salary from employees e WHERE salary = (SELECT min(salary) FROM employees);

SELECT salary from employees WHERE last_name = 'jones';

-- 查询工资比Jones高的员工姓名和工资
SELECT e.first_name,e.last_name ,e.salary FROM employees e WHERE e.salary > (SELECT b.salary from employees b WHERE b.last_name = 'jones');

-- 查询1999年入职公司的所有员工中的最高工资的员工
SELECT e.first_name ,e.last_name ,e.hire_date,e.salary from employees e WHERE salary >= (SELECT max(salary) from employees WHERE year(hire_date) = '1999') and year(hire_date) = '1999' ;

SELECT salary from employees WHERE job_id = 'IT_PROG';

SELECT employee_id ,last_name ,job_id,salary from employees WHERE salary < ANY(SELECT salary from employees WHERE job_id = 'IT_PROG') and job_id <> 'IT_PROG';

-- 查询last_name king 管理的员工
select first_name ,last_name ,manager_id ,employee_id from employees WHERE manager_id in 
(SELECT employee_id  from employees WHERE last_name = 'king');

-- 查询曾经做过ST_CLERK的员工信息
select first_name ,last_name ,manager_id ,employee_id from employees WHERE employee_id in  (SELECT DISTINCT employee_id from job_history WHERE  job_id = 'ST_CLERK');

-- 查询平均工资高于公司平均工资的部门信息
SELECT * from departments WHERE department_id in (
SELECT department_id from employees GROUP BY department_id HAVING AVG(salary) >  (SELECT AVG(salary) from employees));

-- 
SELECT  t.fullname ,t.salarys ,t.allsalary from 
(SELECT CONCAT(first_name,',',last_name) fullname ,salary*12 salarys ,salary*12*(1+IFNULL(commission_pct,0)) allsalary from employees) t WHERE t.allsalary > 150000;

select * from(
select e.first_name,e.last_name,d.department_name 
from employees e left join departments d on e.department_id = d.department_id
UNION
select e.first_name,e.last_name,d.department_name
from employees e right join departments d on e.department_id = d.department_id
)t;


-- 1,备份employees表到employees_bck
CREATE TABLE employees_bak as SELECT * FROM employees;
-- 2,创建employees_bck2表,复制employees表结构
CREATE TABLE employees_bak2 as SELECT * FROM employees where 1=2;
-- 3,备份employees表中工资大于10000的员工信息;
CREATE TABLE employees_bak3 as SELECT * FROM employees where salary > 10000;
-- 3,使用employees表中的某几列创建employees_smallest表(id,fullname,hiredate,salary)。
CREATE TABLE employees_smallest as SELECT employee_id as id , CONCAT(first_name,',',last_name) as fullname ,hire_date as hiredate, salary from employees;

微信扫码关注java技术栈,每日更新面试题目和答案,并获取Java面试题和架构师相关题目和视频。

上一篇下一篇

猜你喜欢

热点阅读