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

每天五道MySQL---1

作者:互联网

  1. 查找最晚入职员工的所有信息

     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 * from employees order by hire_date desc limit 1 
  2. 查找入职员工时间排名倒数第三的员工所有信息

    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 * from employees order by hire_date desc limit 1 offset 2(offset:第几行记录,从0开始)
  3. 查找所有已经分配部门的员工的last_name和first_name

    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 `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 last_name,first_name,dept_no 
    from dept_emp join  employees
    on dept_emp.emp_no = employees.emp_no
  4. 查找所有员工的last_name和first_name以及对应部门编号dept_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 `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 last_name,first_name,dept_no 
    from employees left join  dept_emp
    on dept_emp.emp_no = employees.emp_no
  5. 查找所有员工入职时候的薪水情况,给出emp_no以及salary, 并按照emp_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`));
    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 e.emp_no,salary 
    from employees e left join salaries s
    on e.emp_no = s.emp_no
    and e.hire_date = s.from_date
    order by e.emp_no desc

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