select * from employees where hire_date=(select max(hire_date) from employees);
select * from employees order by hire_date desc limit 0,1; //LIMIT m,n : 表示从第m+1条开始,取n条数据;
PRIMARY KEY (`emp_no`));
涉及到重复情况的考虑 distinct 修饰hire_date 字段 并排序选取第2位(因为最初从0开始计数)
select * from employees
where hire_date=(select distinct hire_date from employees order by hire_date desc limit 2,1);
CREATE TABLE `employees` (PRIMARY KEY (`emp_no`));
select s.*, d.dept_no from salaries s , dept_manager d
where s.to_date='9999-01-01' and d.to_date='9999-01-01' and s.emp_no = d.emp_no;
使用 内连接 inner join on 也可以~一个道理~
select s.*,d.dept_no from salaries s inner join dept_manager d on s.emp_no =d.emp_no and s.to_date='9999-01-01' and d.to_date='9999-01-01';
//一定注意对应的顺序关系!!
CREATE TABLE `dept_manager` (PRIMARY KEY (`emp_no`,`from_date`));
常规做法:
只有一列公共列且名称 类型均相同 则用自然连接:
select e.last_name,e.first_name,d.dept_no from employees e natural join dept_emp d ; CREATE TABLE `dept_emp` (PRIMARY KEY (`emp_no`));
左连接:
select e.last_name ,e.first_name, d.dept_no from employees e left join dept_emp d on e.emp_no=d.emp_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`));
2、where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。
常规做法:
select s.emp_no ,s.salary from salaries s,employees e where s.emp_no=e.emp_no and s.from_date=e.hire_date order by e.emp_no desc;
内连接:
select s.emp_no ,s.salary from salaries s inner join employees e on s.emp_no=e.emp_no and s.from_date=e.hire_date order by e.emp_no desc;
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 emp_no, count(emp_no) t from salaries group by emp_no having t>15;
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,PRIMARY KEY (`emp_no`,`from_date`));
使用distinct修饰字段 去除重复:
select distinct salary from salaries where to_date='9999-01-01' order by salary desc;
使用group by 来分组去重复:
select salary from salaries where to_date='9999-01-01' group by salary order by salary desc;
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 d.dept_no ,d.emp_no ,s.salary from dept_manager d,salaries s where d.emp_no=s.emp_no and d.to_date='9999-01-01' and s.to_date='9999-01-01';
内连接:
select d.dept_no,d.emp_no,s.salary from dept_manager d inner join salaries s on d.emp_no=s.emp_no and d.to_date='9999-01-01' and s.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`));
常规not in 方法:
select emp_no from employees where emp_no not in(select emp_no from dept_manager);
用 左连接的方法:
select e.emp_no from employees e left join dept_manager d on e.emp_no=d.emp_no where d.emp_no is null;
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,PRIMARY KEY (`emp_no`));
常规:
select d.emp_no , m.emp_no as manager_no from dept_emp d, dept_manager m where d.dept_no=m.dept_no and d.emp_no<>m.emp_no and d.to_date='9999-01-01' and m.to_date='9999-01-01';
内连接:
select d.emp_no , m.emp_no manager_no from dept_emp d inner join dept_manager m on d.dept_no=m.dept_no and d.to_date='9999-01-01' and m.to_date='9999-01-01' and d.emp_no !=m.emp_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`));
注意 max聚合函数----分组group by ----别名:
select d.dept_no,d.emp_no,max(s.salary) as salary from dept_emp d inner join salaries s on d.emp_no=s.emp_no and d.to_date='9999-01-01' and s.to_date='9999-01-01' group by d.dept_no;
使用 分组后再限制条件 having :
select d.dept_no,d.emp_no, s.salary as salary from dept_emp d inner join salaries s on d.emp_no=s.emp_no and d.to_date='9999-01-01' and s.to_date='9999-01-01' group by d.dept_no having s.salary=max(salary);
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,PRIMARY KEY (`emp_no`,`from_date`));
写法一:
1
2
3
4
5
6
7
8
9
10
11
12
|
select dept_emp.dept_no as dept_no_a, dept_emp.emp_no, max(salaries.salary) as salary
from dept_emp,salaries
where salaries.emp_no=dept_emp.emp_no
group by dept_emp.emp_no,dept_emp.dept_no
having max(salaries.salary) =
(
select max(salaries.salary)
from dept_emp inner join salaries
on salaries.emp_no=dept_emp.emp_no
where dept_emp.dept_no = dept_no_a
)
order by dept_no_a
|
写法二:
1
2
3
4
5
6
7
8
9
10
11
12
|
select distinct dept_no, s.emp_no, salary
from dept_emp as d inner join salaries as s
on s.emp_no = d.emp_no and s.salary =
(select s2.salary
from salaries as s2 inner join dept_emp as d2
on s2.emp_no = d2.emp_no
where d2.dept_no = d.dept_no
order by s2.salary desc
limit
1
)
order by dept_no
;
|
写法三:
受限于any_value的实现,如果是返回第一个值就没有问题
1
2
3
4
5
6
7
|
select ret.dept_no, any_value(ret.emp_no), max(ret.salary)
from
(select dept_emp.dept_no,dept_emp.emp_no, max(salaries.salary) as salary
from dept_emp,salaries
where salaries.emp_no=dept_emp.emp_no
group by dept_emp.emp_no,dept_emp.dept_no order by salary desc
) as ret group by ret.dept_no
|
`to_date` date DEFAULT NULL);
select title ,count(distinct emp_no) as t from titles group by title having t>=2 ;
CREATE TABLE IF NOT EXISTS "titles" (
`emp_no` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`from_date` date NOT NULL,
`to_date` date DEFAULT NULL);
select * from employees where emp_no %2=1 and last_name!= 'Mary' order by hire_date desc ;
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,PRIMARY KEY (`emp_no`));
select t.title, avg(s.salary) as avg from titles t, salaries s where t.emp_no=s.emp_no and t.to_date='9999-01-01' and s.to_date='9999-01-01' group by t.title;
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,`to_date` date DEFAULT NULL);
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, max(s.salary), e.last_name, e.first_name from employees e, salaries s where e.emp_no=s.emp_no and s.to_date='9999-01-01' and s.salary not in(select max(salary) from salaries where to_date='9999-01-01') ;
或者是 直接在小于最大的里面再找出最小的那个即可:
select e.emp_no, max(s.salary), e.last_name, e.first_name from employees e, salaries s where e.emp_no=s.emp_no and s.to_date='9999-01-01' and s.salary <(select max(salary) from salaries where to_date='9999-01-01') ;
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 em.last_name ,em.first_name, de.dept_name from employees em left join dept_emp dp on em.emp_no=dp.emp_no left join departments de on de.dept_no=dp.dept_no;
CREATE TABLE `departments` (
`dept_no` char(4) NOT NULL,
`dept_name` varchar(40) NOT NULL,
PRIMARY KEY (`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 max(salary)-min(salary) as growth from salaries where emp_no=10001 ;
严谨的做法: 找到第一次记录和最后一次记录
select (select salary from salaries where emp_no=10001 order by to_date desc limit 0,1)-(select salary from salaries where emp_no=10001 order by to_date limit 0,1)as growth ;
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 , b.salary-c.salary as growth from employees e
inner join salaries b on e.emp_no=b.emp_no and b.to_date='9999-01-01'
inner join salaries c on c.emp_no=e.emp_no and e.hire_date=c.from_date
order by growth ;
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 de.dept_no ,dp.dept_name, count(s.salary) as sum from dept_emp de join departments dp on dp.dept_no=de.dept_no join salaries s on s.emp_no=de.emp_no group by de.dept_no;
CREATE TABLE `departments` (
`dept_no` char(4) NOT NULL,
`dept_name` varchar(40) NOT NULL,
PRIMARY KEY (`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 `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`));
23