如何检查年度最佳员工

时间:2013-04-20 14:13:35

标签: php mysql time-and-attendance

这是我的MySQL表:

_______________________________________________
Id | Name| Employee id | Date       | Attendance
_______________________________________________
1  | xyz |     196     | 2013-04-01 | present  
2  | xyz |     196     | 2013-04-02 | present  
3  | xyz |     196     | 2013-04-03 | present  
4  | xyz |     196     | 2013-04-04 | absent  
5  | xyz |     196     | 2013-04-05 | present  
6  | abc |     197     | 2013-04-01 | present

7  | abc |     197     | 2013-04-02 | present  
8  | abc |     197     | 2013-04-03 | present  
9  | abc |     197     | 2013-04-04 | present  
10 | abc |     197     | 2013-04-05 | present

_______________________________________________

我想算一下员工大多数在场的日子,我想在PHP中得到这样的结果:

___________________________________________________________
 Name| Employee id| Attendance     | Best OR NOT
___________________________________________________________
 xyz |  196       | 4 Present days |

 abc |  197       | 5 Present days | This is best employees of the year

__________________________________________________________________________

我该怎么做?

5 个答案:

答案 0 :(得分:1)

<强> SQL

SELECT name, employeeID count(userID) as datesAttend FROM Attendance GROUP BY name

PHP(mysql类方法,只是一个例子)

/* mysql_query example ( use beter db class ) */

$best = 0;
$bestEmployee = '';

$query = mysql_query("SELECT Name, EmployeeID, count(EmployeeID) as datesAttend FROM Attendance GROUP BY EmployeeID");

while($row = mysql_fetch_array($query)) {
  echo $row['Name']. " " .$row['EmployeeID']. " " .$row['datesAttend'];
  if($row['datesAttend'] > $best) {
   $best = $row['datesAttend'];
   $bestEmployee = $row['EmployeeID'];
  }
}

echo $bestEmployee ." is employee of the year!!!";
throw PartyForEmployee();

实施例: http://www.sqlfiddle.com/#!2/9d282/2/0

答案 1 :(得分:0)

像这样的查询:

select employeeId, count(employeid) as num from table_name group by attendence limit 1 

会给你最好的工人ID。您可以通过将表格插入正确的位置来编辑表格。你能再解释一下你的问题吗?

//DB_HOST etc. are db connection constants
$dbc=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$data = mysql_query('your query') or die(mysql_error());
// A COUNT query will always return 1 row
// Use fetch_assoc it's easier to use
$row = mysql_fetch_assoc($data);
$bestId = $row['employeeId'];
$bestDayNumber = $row['num'];

答案 2 :(得分:0)

此查询将为您提供最佳员​​工:

select Name, "Employee id", count(*) 
from table_name 
where Attendance = "present" 
group by Name 
order by count(*) desc 
limit 1;

“group by”子句意味着您希望每个员工都有单独的结果。 “where”条款确保您不会计算他们缺席的日子。 “order by”条款赋予员工最多的日子。 “限制”条款只是第一个,因为你只想要最好的。

答案 3 :(得分:0)

尝试此查询..

SELECT *, COUNT(employee_id) AS cnt, CASE WHEN employee_id = (SELECT employee_id FROM employees
WHERE 1 GROUP BY  employee_id ORDER BY COUNT(employee_id) DESC LIMIT 0 , 1)   THEN 'Best' 
ELSE 'Not' END AS avl
FROM  `employees`
WHERE 1 
GROUP BY employee_id

根据您的需要更改表名称和字段名称

答案 4 :(得分:-1)

试试这个。

   $query = mysql_query("select name ,`Employee id` as emplyee_id, CONCAT(count(`Employee id`),' Present days') as Attendance
   from Table1 
   where `Attendance` = 'present' group by `Employee id` ");



   while($row = mysql_fetch_array($query)) {
  echo $row['name']. " - " .$row['emplyee_id']. " - " .$row['Attendance']."</ br>";
 }

DEMO HERE