-2

这是我的 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

__________________________________________________________________________

我怎样才能做到这一点?

4

5 回答 5

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

于 2013-04-20T14:31:22.763 回答
0

像这样的查询:

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

会给你最好的工人身份证。您可以通过将表格插入到适当的位置来对其进行编辑。你能再解释一下你的问题吗?

//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'];
于 2013-04-20T14:17:52.620 回答
0

此查询将为您提供最好的员工:

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

“分组依据”子句意味着您希望每个员工都有一个单独的结果。“where”子句确保您不计算他们缺席的天数。“order by”子句将在场天数最多的员工放在首位。“限制”子句只得到第一个,因为你只想要最好的。

于 2013-04-20T14:25:38.867 回答
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

根据需要更改表名和字段名

于 2013-04-20T14:33:53.673 回答
-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>";
 }

在这里演示

于 2013-04-20T14:34:42.967 回答