计算员工的经验

时间:2014-02-17 12:21:57

标签: sql sql-server

员工的总计数经验,并且还显示具有超过5年经验的员工......

请帮帮我。 先谢谢你。

SELECT tbl_Employee.EFirstName,
       tbl_Employee.EMiddleName,
       tbl_Employee.ELastName,
       tbl_Empl‌​oyee.EmployeeID, 
       tbl_BranchMaster.BranchName,
       tbl_DepartmentMaster.DepartmentName,
       tbl_StateMaster.‌​StateName,
       tbl_Employee.EmployeementDate,
       dbo.FN_Basic(tbl_Employee.EmployeeID) AS basic,
       YEAR(CURDATE())-YEAR(tbl_Employee.EmployeementDate) AS Age
FROM tbl_StateMaster 
INNER JOIN tbl_DepartmentMaster 
INNER JOIN tbl_EmployeeStructure 
INNER JOIN tbl_Employee ON tbl_EmployeeStructure.EmployeeID = tbl_Employee.EmployeeID 
INNER JOIN tbl_BranchMaster ON tbl_EmployeeStructure.BranchID = tbl_BranchMaster.BranchID 
                            ON tbl_DepartmentMaster.DepartmentID = tbl_EmployeeStructure.DepartmentID 
                            ON tbl_StateMaster.StateID =tbl_EmployeeStructure.StateID

1 个答案:

答案 0 :(得分:2)

DROP TABLE IF EXISTS employee;

CREATE TABLE employee(id INT , joining_date DATETIME);

INSERT INTO `employee` (`id`, `joining_date`) VALUES ('1', NOW()),('2',NOW() - INTERVAL 6 YEAR);

SELECT * FROM employee;
+------+---------------------+
| id   | joining_date        |
+------+---------------------+
|    1 | 2014-02-17 17:56:53 |
|    2 | 2008-02-17 17:56:53 |
+------+---------------------+
2 rows in set (0.00 sec)

SELECT id,joining_date FROM employee WHERE DATEDIFF(DATE(NOW()),DATE(joining_date))/365 > 5;
+------+---------------------+
| id   | joining_date        |
+------+---------------------+
|    2 | 2008-02-17 17:56:53 |
+------+---------------------+
1 row in set (0.01 sec)

您也可以尝试

SELECT id,joining_date,DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(joining_date)),'%Y')+0 AS YearsOfExp FROM employee WHERE DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(joining_date)),'%Y')+0 > 5;
+------+---------------------+------------+
| id   | joining_date        | YearsOfExp |
+------+---------------------+------------+
|    2 | 2008-02-17 17:56:53 |          6 |
+------+---------------------+------------+
1 row in set (0.00 sec)

为了更好地使用索引,您可以像

一样编写它
SELECT * FROM employee WHERE joining_date <= DATE_ADD(NOW(), INTERVAL -5 YEAR);