数据库
首页 > 数据库> > 每天五道MySQL---2

每天五道MySQL---2

作者:互联网

  1. 找出所有员工当前(to_date='9999-01-01')具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示

    CREATE TABLE `salaries` (
    `emp_no` int(11) NOT NULL,
    `salary` int(11) NOT NULL,
    `from_date` date NOT NULL,
    `to_date` date NOT NULL,
    PRIMARY KEY (`emp_no`,`from_date`));
    ---------------------------------
    select distinct salary 
    from salaries
    where to_date='9999-01-01'
    order by salary desc
  2. 获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示to_date='9999-01-01'

    CREATE TABLE `dept_manager` (
    `dept_no` char(4) NOT NULL,
    `emp_no` int(11) NOT NULL,
    `from_date` date NOT NULL,
    `to_date` date NOT NULL,
    PRIMARY KEY (`emp_no`,`dept_no`));
    CREATE TABLE `salaries` (
    `emp_no` int(11) NOT NULL,
    `salary` int(11) NOT NULL,
    `from_date` date NOT NULL,
    `to_date` date NOT NULL,
    PRIMARY KEY (`emp_no`,`from_date`));
    ----------------------------------
    select dept_no,salaries.emp_no,salary
    from salaries left join dept_manager
    on salaries.emp_no = dept_manager.emp_no
    where  dept_manager.to_date ='9999-01-01'
    and salaries.to_date ='9999-01-01'
  3. 获取所有非manager的员工emp_no

    CREATE TABLE `dept_manager` (
    `dept_no` char(4) NOT NULL,
    `emp_no` int(11) NOT NULL,
    `from_date` date NOT NULL,
    `to_date` date NOT NULL,
    PRIMARY KEY (`emp_no`,`dept_no`));
    CREATE TABLE `employees` (
    `emp_no` int(11) NOT NULL,
    `birth_date` date NOT NULL,
    `first_name` varchar(14) NOT NULL,
    `last_name` varchar(16) NOT NULL,
    `gender` char(1) NOT NULL,
    `hire_date` date NOT NULL,
    PRIMARY KEY (`emp_no`));
    ------------------------------------
    select distinct employees.emp_no 
    from employees 
    where employees.emp_no  not in (
    select distinct emp_no from dept_manager
    )
  4. 获取所有员工当前的manager,如果当前的manager是自己的话结果不显示,当前表示to_date='9999-01-01'。
    结果第一列给出当前员工的emp_no,第二列给出其manager对应的manager_no。

    CREATE TABLE `dept_emp` (
    `emp_no` int(11) NOT NULL,
    `dept_no` char(4) NOT NULL,
    `from_date` date NOT NULL,
    `to_date` date NOT NULL,
    PRIMARY KEY (`emp_no`,`dept_no`));
    CREATE TABLE `dept_manager` (
    `dept_no` char(4) NOT NULL,
    `emp_no` int(11) NOT NULL,
    `from_date` date NOT NULL,
    `to_date` date NOT NULL,
    PRIMARY KEY (`emp_no`,`dept_no`));
    ---------------------------------
    select dept_emp.emp_no,dept_manager.emp_no as manager_no
    from dept_emp join dept_manager
    where dept_emp.dept_no = dept_manager.dept_no
    and dept_emp.to_date =  dept_manager.to_date
    and dept_emp.emp_no <> dept_manager.emp_no
  5. 获取所有部门中当前员工薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary

    CREATE TABLE `dept_emp` (
    `emp_no` int(11) NOT NULL,
    `dept_no` char(4) NOT NULL,
    `from_date` date NOT NULL,
    `to_date` date NOT NULL,
    PRIMARY KEY (`emp_no`,`dept_no`));
    CREATE TABLE `salaries` (
    `emp_no` int(11) NOT NULL,
    `salary` int(11) NOT NULL,
    `from_date` date NOT NULL,
    `to_date` date NOT NULL,
    PRIMARY KEY (`emp_no`,`from_date`));
    ---------------------------------
    select dept_no,salaries.emp_no,Max(salaries.salary) as salary
    from salaries join dept_emp
    on salaries.emp_no = dept_emp.emp_no
    where salaries.to_date = dept_emp.to_date
    and salaries.to_date = '9999-01-01'
    group by dept_no

标签:每天,no,五道,dept,manager,emp,MySQL,date,NULL
来源: https://www.cnblogs.com/luo-bo/p/11134296.html