在Employee表中,如何根据加入列的日期更新所有员工的体验列

时间:2016-09-09 15:47:32

标签: mysql sql-server

我有一个Employee表,我想根据加入DOJ列的日期更新所有员工的体验列。

+----+---------+------------+-------------+------------+
| Id |  name   |    DOJ     | Designation | Experience |
+----+---------+------------+-------------+------------+
|  1 | Krishna | 14/01/2012 | Developer   |            |
|  2 | Rahim   | 14/01/2013 | Developer   |            |
|  3 | Ram     | 14/01/2014 | Developer   |            |
|  4 | Shyam   | 14/01/2015 | Developer   |            |
|  5 | Ashok   | 14/01/2016 | Developer   |            |
+----+---------+------------+-------------+------------+

1 个答案:

答案 0 :(得分:0)

年计算的经验

UPDATE test SET Experience = floor(datediff(curdate(),DOJ) / 365)

经验月计算

UPDATE test SET Experience = 12 * (YEAR(Curdate()) - YEAR(DOJ)) + (MONTH(curdate()) - MONTH(DOJ))