mysql update

#把每个员工编号和上司的编号+1,用order by 完成
update t_emp set empno = empno + 1,mgr = mgr + 1
ORDER BY empno DESC

# 把月收入前三名的工资减100 LIMIT 完成
UPDATE t_emp set sal = sal - 100
ORDER BY sal+ IFNULL(comm,0) desc
limit 3

# 把 10 部门中,工龄超过20年的员工,底薪增加200
update t_emp set comm = IFNULL(comm,0) + 200
where deptno = 10 AND DATEDIFF(NOW(),hiredate)/365 > 20

#把 ALLEN 调往RESEARCH部门,职务调整为ANALYST

第一种方法
UPDATE t_emp SET job = 'ANALYST' ,
deptno = (SELECT deptno FROM t_dept WHERE
dname = 'RESEARCH')
WHERE ename = 'ALLEN'

第二种方法

#把 ALLEN 调往RESEARCH部门,职务调整为ANALYST
UPDATE t_emp e join t_dept d
SET e.deptno = d.deptno
WHERE e.ename = 'ALLEN' and d.dname = "OPERATIONS"

#把底薪低于公司平均底薪的员工,底薪增加150
UPDATE t_emp e join
(select avg(sal) avg FROM t_emp) t
on e.sal < t.avg
set sal = sal + 150

#把没有部门的员工 或者sales 部门低于2000元的员工 都调往 20部门
update t_emp e left join t_dept d
on e.deptno = d.deptno
set e.deptno = 20
where e.deptno is null or (d.dname = "SALES" and e.sal < 2000 )

原文地址:https://www.cnblogs.com/ericblog1992/p/11321824.html