我正在制定一个程序,该程序根据每个员工的出勤情况来计算其用餐津贴。这是我现在拥有的数据,该表称为出勤。该表是从我导入的csv文件填充的,这是我导入csv的代码
function uploadData()
{
$fp = fopen($_FILES['userfile']['tmp_name'],'r') or die("can't open file");
// Ignore header line
$header = fgetcsv($fp);
// Array to store the partial records
$attendance= [];
while($csv_line = fgetcsv($fp))
{
// Key to identify first part of record (Name and date)
$key = $csv_line[1]."/".$csv_line[3];
if ( isset($attendance[$key]) ){
$start = $attendance[$key];
// Extract data from first part of record and add in end date from current row
$data = array(
'id_attend' => $start[0] ,
'emp_code' => $start[1],
'emp_name' => $start[2],
'date' => $start[3],
'time_in' => $start[4],
'time_out' => $csv_line[4],
);
$data['crane_features']=$this->db->insert('attendance', $data);
// Remove partial record
unset($attendance[$key]);
}
else {
// Store partial record
$attendance[$key] = $csv_line;
}
}
fclose($fp) or die("can't close file");
$data['success']="success";
}
这是我的桌子
| id_attend | emp_code | emp_name | date | time_in | time_out |
----------------------------------------------------------------------------------
| 0001 | brw | brown |01.01.2001| 07.00 | 20.00 |
| 0002 | cny | cony |01.01.2001| 07.00 | 20.00 |
----------------------------------------------------------------------------------
我需要计算他们参加多少天,并将其保存在我的数据库中。我已经有一个表格作为名为 allowance
的容器------------------------------------------------------------------------
|id_allowance(auto increment)|emp_code|emp_name|days_attended|allowance|
------------------------------------------------------------------------
| 0001 | brw | brown | 1 | 30.00 |
| 0002 | cny | cony | 1 | 30.00 |
------------------------------------------------------------------------
有什么方法可以在每次导入新的csv文件时自动生成和更新配额吗?因为配额表最初应该是空的,并且实际上取决于出勤表
答案 0 :(得分:2)
为此,您需要在MySQL中创建一个触发器,以便当新数据插入出勤表时,同一查询将自动生成并运行 allow 表
DROP TRIGGER IF EXISTS allowance_insert_trigger;
DELIMITER $$
CREATE TRIGGER allowance_insert_trigger
AFTER INSERT ON attendance FOR EACH ROW
begin
DECLARE days_attandence varchar(10);
SELECT count(*) INTO days_attandence FROM attendance WHERE emp_code=NEW.emp_code GROUP BY date;
INSERT INTO `allowance` ( `emp_code`, `emp_name`, `days_attended`, `allowance`) VALUES ( NEW.emp_code, NEW.emp_name, days_attandence, '30.00');
END;
$$
DELIMITER ;
您要编写这样的触发器,在这里我将静态值作为津贴,但您可以在其中放入自己的计算值。