我有 2 张桌子员工和部门
表格employee
列
empid
name
salary
location
表格department
列
deptno
deptname
empid
mangerid
现在我想找到部门方面的最高薪水,然后将它们增加 2000
ed:哈我没想到您实际上是要更新行,我以为您只是想检索值。@Ajith Sasidharan 的回答更全面。
您的部门表引用员工表而不是相反似乎很奇怪,但是这应该可以满足您的要求:
SELECT
d.deptno,
d.deptname,
max(e.salary+2000)
FROM
department d
INNER JOIN
employee e
ON
e.empid = d.empid
GROUP BY
d.deptno, d.deptname;
update emp
set sal = sal + 2000
where empid in (
select empid from dept where (deptno,sal) in (
select d.deptno,max(e.sal)
from emp e,
dept d
where e.empid = d.empid
group by deptno));
select e.name,e.salary,d.name as Department
from employee e join dept d on e.dept = d.id
where (e.salary, e.dept) IN (
select max(salary), dept
from employee join dept on (employee.dept = dept.id)
group by employee.dept
);