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 )


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM