查找各个部门当前(dept_manager.to_date=‘9999-01-01‘)领导当前(salaries.to_date=‘9999-01-01‘)薪水详情以及其对应部门编号dept_no
(注:输出结果以salaries.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`));
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL, -- ‘部门编号‘
`emp_no` int(11) NOT NULL, -- ‘员工编号‘
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
SELECT salaries.emp_no,salary,from_date,dept_manager.to_date,dept_no FROM salaries
INNER 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‘
ORDER BY salaries.emp_no;
查找所有已经分配部门的员工的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
inner join dept_emp on employees.emp_no=dept_emp.emp_no